Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by qq (Hermit)
on Feb 18, 2005 at 10:29 UTC ( [id://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?

Replies are listed 'Best First'.
Re^2: Looping through a hash where some keys are the same
by lidden (Curate) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://432254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found