in reply to Re^3: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
in thread Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
To the question in your previous post I would say that should work.
So would I and the line does look rather nice. Unfortunately, it doesn't seem to work as you might expect. It does look as if it is resetting the iterator but it is failing to consistently retrieve the values from the hash, only managing to get the value for key "six" twice and not getting any others.
knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my %hash = ( > one => q{ein}, > two => q{zwei}, > six => q{sechs}, > ); > say qq{@{ [ %hash ] }}; > for ( 1 .. 9 ) > { > my ( $k, $v ) = each %hash || each %hash; > say qq{$_: $k => $v} > }' six sechs one ein two zwei Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 1: six => Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 2: one => Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 3: two => 4: six => sechs Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 5: one => Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 6: two => 7: six => sechs Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 8: one => Use of uninitialized value $v in concatenation (.) or string at -e lin +e 11. 9: two => knoppix@Microknoppix:~$
I can't puzzle out what is going on here. I wondered if it was a precedence problem but changing each %hash to each( %hash ) made no difference.
Can anyone shed any light on what's happening here?
Update: A different approach to prove to myself that the each would behave as described.
knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my %hash = ( > one => q{ein}, > two => q{zwei}, > six => q{sechs}, > ); > say qq{@{ [ %hash ] }}; > my $ct; > for ( 1 .. 3 ) > { > while ( my ( $k, $v ) = each %hash ) > { > $ct ++; > say qq{$ct: $k => $v} > } > }' six sechs one ein two zwei 1: six => sechs 2: one => ein 3: two => zwei 4: six => sechs 5: one => ein 6: two => zwei 7: six => sechs 8: one => ein 9: two => zwei knoppix@Microknoppix:~$
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
by jethro (Monsignor) on Jan 29, 2011 at 01:09 UTC | |
by johngg (Canon) on Jan 29, 2011 at 18:53 UTC | |
by puterboy (Scribe) on Jan 29, 2011 at 23:11 UTC |