in reply to Re^3: Hash value interpolation
in thread Hash value interpolation


Kyle wrote "The real problem with each is that every hash has only one iterator, and if you drop out of a loop that's walking the hash, the next loop that tries to walk that hash will start where the first left off."

Can you give an example where the above mentioned happens or point me to an example.
Thanks very much

Replies are listed 'Best First'.
Re^5: Hash value interpolation
by ikegami (Patriarch) on Feb 18, 2009 at 01:29 UTC
    local $\ = "\n"; my %h = map { $_ => 1 } qw( a b c d ); for my $pass (1..3) { print "Pass $pass:"; while (my ($k,$v) = each %h) { print $k; last if $pass == 1; } }
    Pass 1: c Pass 2: a <---- 'c' missing b d Pass 3: c a b d
    Solution:
    local $\ = "\n"; my %h = map { $_ => 1 } qw( a b c d ); for my $pass (1..3) { print "Pass $pass:"; keys %h; # Reset iterator while (my ($k,$v) = each %h) { print $k; last if $pass == 1; } }
    Pass 1: c Pass 2: c <---- Welcome back a b d Pass 3: c a b d
      ikegami thanks again for all the help and explanations