Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Unique Array Items

by thelenm (Vicar)
on Nov 06, 2003 at 21:59 UTC ( #305170=note: print w/ replies, xml ) Need Help??


in reply to Re: Unique Array Items
in thread Unique Array Items

In this code:

my %h; my @toBeMoved = sort {$a <=> $b} grep {++$h{$_} == 1} @toBeMoved;

the value associated with each element is incremented (not set to 1, that's important). So the grep block will only return a true value the first time a value is seen. E.g., the first time 133 is seen, ++$h{133} == 1 will be true. The second time 133 is seen, the block will return a false value, since $h{133} will now be 2, not 1. The block will only return a true value once for each unique value. Does that clear things up at all?

-- Mike

--
XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
-- grantm, perldoc XML::Simpler


Comment on Re: Re: Unique Array Items
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Re: Unique Array Items
by Art_XIV (Hermit) on Nov 06, 2003 at 22:05 UTC

    That does clear it up! Thank you very much thelenm! :)

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-07-28 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (251 votes), past polls