in reply to Re: (FIRST|NEXT)KEY in context
in thread (FIRST|NEXT)KEY in context

Observe an infinite loop
my %foo = 1 .. 10; while(my($k,$v) = each %foo){ print "k($k)v($v)\n"; } while(my($k,$v) = each %foo){ print "k($k)v($v)", scalar keys %foo, "\n"; } __END__ k(1)v(2) k(3)v(4) k(7)v(8) k(9)v(10) k(5)v(6) k(1)v(2)5 k(1)v(2)5 k(1)v(2)5 k(1)v(2)5 k(1)v(2)5 k(1)v(2)5 ...
See http://search.cpan.org/~dapm/perl-5.10.1/pod/perlfunc.pod#each, http://search.cpan.org/~dapm/perl-5.10.1/pod/perlfunc.pod#keys, http://search.cpan.org/~dapm/perl-5.10.1/pod/perlfunc.pod#values

this thread is 6 years old; nevertheless, i can't make each() work on a tied hash :(

If you /msg janitors can you please SOPWify [id://822822], they will turn your reply into a thread of its own.

Replies are listed 'Best First'.
Re^3: (FIRST|NEXT)KEY in context
by johnvk (Initiate) on Feb 12, 2010 at 13:25 UTC

    i'm not sure if you were answering my question or trying to ask a question yourself.

    In your case, the reason why you get an infinite loop is that the keys() call resets the iterator that is shared among each(), keys() and values();. so in your loop the iterator gets set to the beginning every iteration by the call to keys()

    but the docs you pointed to explain that. so i presume you knew that. if this is supposed to be an answer to my query, i aplogize but i dont understand. in particular, my issue only comes up when the hash is tied.

    As for moving my query to its own thread, i actually wantd it here, because it appears the OP and i are asking the same question. I figured i'd keep perlmonks organized by keeping the topic all in one place. Are you saying i'll get less attn as a reply rather than an original post?

    Thanks.

      in particular, my issue only comes up when the hash is tied.

      Because you call keys immediately before calling each.