Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Looping through a hash where some keys are the same

by qq (Hermit)
on Feb 18, 2005 at 10:29 UTC ( #432254=note: print w/ replies, xml ) Need Help??


in reply to Looping through a hash where some keys are the same

Does anyone else find it surprising that each returns twice? I would have thought that the second assignment of the duplicate key would overwrite the original, and you only get one key/value pair back.

I appeal to the wise among you...

#!/usr/bin/perl use Data::Dumper; my %h = ( a => 1, a => 2 ); print Dumper \%h; for ( my($k,$v) = each %h ) { print "$k -> $v\n"; } __END__ $VAR1 = { 'a' => 2 }; a -> 2 a -> 2 # why?


Comment on Re: Looping through a hash where some keys are the same
Select or Download Code
Re^2: Looping through a hash where some keys are the same
by lidden (Deacon) on Feb 18, 2005 at 10:42 UTC
    each is returning a two element list, hence it is looping two times and $k, $v is only set once. It works as expected with a while. See "perldoc -f each".

      I new I was overlooking something obvious - thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2014-10-26 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls