array - removing duplicates

J

Jerry Preston

@dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );

I want to removes all the duplicates. I am currently using:

@dat = grep { !$dat{ $_ }++ } @dat;

99% of the time I get an empty array. Why? Is their a better/100% way?

Thanks,

Jerry
 
T

Tony Curtis

@dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24
); I want to removes all the duplicates. I am currently
using:
@dat = grep { !$dat{ $_ }++ } @dat;
99% of the time I get an empty array. Why?

I guess something is broken on your system. Why "99%"? What
happens the other 1% of the time?

Or there's something else going on you're not telling us about
(maybe because you thought it wasn't important and
accidentally omitted salient details of a larger problem).

hth
t
 
J

Jerry Preston

Bob,

Syntax!

Thanks,

Jerry


Bob Walton said:
Your test program, verbatim as above, generates the proper list of
numbers without duplicates 100% of the times I have run it (at least
half a dozen times). The problem you are having probably lies elsewhere.
 
J

Jürgen Exner

Jerry said:
@dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );

I want to removes all the duplicates. I am currently using:

Do you have any particular problem with "perldoc -q duplicate"?
" How can I remove duplicate elements from a list or array?"

jue
 
A

Anno Siegel

Bob Walton said:
Your test program, verbatim as above, generates the proper list of
numbers without duplicates 100% of the times I have run it (at least
half a dozen times). The problem you are having probably lies elsewhere.

My guess is, the (undeclared) hash %dat still contains data from an
earlier run in 99 % of the cases. Solution: use strict.

Anno
 
U

Uri Guttman

AS> My guess is, the (undeclared) hash %dat still contains data from an
AS> earlier run in 99 % of the cases. Solution: use strict.

i guessed that too. strict won't help unless the declaration is in the
tightest scope and it will clear the hash before the uniquing line. if
it is declared outside a loop doing the unique it will have the same
bug.

and the OP never stated what was wrong with the results. typical poor
description of the problem. if we were given some data from a bad result
then this guess could be made more assuredly or disregarded.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top