Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Unique Array Items

by tcf22 (Priest)
on Nov 06, 2003 at 21:31 UTC ( [id://305161]=note: print w/replies, xml ) Need Help??


in reply to Unique Array Items

Use ++$h{$_} instead of $h{$_}++ so the incrementing is done first, before the compare. $h{$_}++ returns the value, which is numerically 0 the first time through, then increments the value. This causes things seen only once to be ignored.
@toBeMoved = sort {$a <=> $b} grep {++$h{$_} == 1} @toBeMoved;

- Tom

Replies are listed 'Best First'.
Re* Unique Array Items
by Roy Johnson (Monsignor) on Nov 06, 2003 at 21:41 UTC
    Or instead of "this is the first occurrence", use "haven't seen this before":
    ...grep {!$h{$_}++}...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305161]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-03-19 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found