LanX has asked for the wisdom of the Perl Monks concerning the following question:
I just realized that it's not possible to have independent each iterators for the same hash.
Is there any "lazy" alternative? I.e. w/o copying all keys in advance into an array?
Arrays are easier since the "keys" can be walked by incrementation like in c-style for-loop.
For illustration
DB<140> sub gen { my $href=shift; return sub { join " => ", each %$href } } DB<141> $iter1 =gen \%h; $iter2 =gen \%h DB<142> @h{a..d}=1..4 => (1, 2, 3, 4) DB<143> print &$iter1,"\t" for 1..3 a => 1 d => 4 c => 3 DB<144> print &$iter2,"\t" for 1..3 b => 2 a => 1 DB<145> print &$iter1,"\t" for 1..3 d => 4 c => 3 b => 2 DB<146> print &$iter2,"\t" for 1..3 a => 1 d => 4
It seems impossible to have independent iterators, and copying the keys in advance isn't really efficient...
I already tried aliasing and referencing the hash, w/o effect it's always the same iterator...
Furthermore, this can cause really ugly bugs ...
Cheers Rolf
( addicted to the Perl Programming Language)
PS:
lanx@nc10-ubuntu:~/perl$ perl -version This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
|
|---|