in reply to Re^2: 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. Nice line by the way. UPDATE: Seems it won't work after all, see johngg's experiment

But the delete bevor add idea won't work, because hashed items get stored into specific slots depending on their hash value. I.e. new passengers always want to go to a seat that depends on their social security number, not to the seat that was vacated last.

What happens when a passengers wants a seat that is occupied, There are two mayor strategies to handle this (that I remember), one is to let more than one passenger sit on a seat, the other is to calculate another seat in a deterministic pattern until the passenger finds a free one (for example the next seat, or the 13th seat from this one). I believe perl hashes use the first method (more than one on a seat) but I'm not sure.

  • Comment on Re^3: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"

Replies are listed 'Best First'.
Re^4: Using 'each' to get random (key, value) pair from hash returns "uninitialized value"
by johngg (Canon) on Jan 29, 2011 at 00:26 UTC
    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

      It did take a while, but I finally got it. The || creates boolean, i.e. scalar context. And each() in scalar context returns only the key and not the index.

        An excellent piece of detective work :-)

        So just grabbing the key alone and pulling the value manually will work.

        knoppix@Microknoppix:~$ perl -Mstrict -wE ' my %hash = ( one => q{ein}, two => q{zwei}, six => q{sechs}, ); say qq{@{ [ %hash ] }}; for ( 1 .. 9 ) { my $k = each %hash || each %hash; say qq{$_: $k => $hash{ $k }}; }' 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:~$

        Thanks for clearing up the mystery.

        Update: Apologies to duelafn, I didn't see his post before making this essentially identical reply.

        Cheers,

        JohnGG