... copying the keys in advance isn't really efficient... My hopes were not to rely on dependencies...
I don't understand the point about real efficiency, but if you want absolutely no dependencies, you can always roll your own iterator. This one simply stops when it's exhausted, but it's easy to imagine simulating the behavior of the each built-in when it reaches the end of iteration.
>perl -wMstrict -le "my %hash = qw( one uno two dos three tres four quatro five cinco ); sub my_each (\%) { my $hr = shift; my @k = keys %$hr; ;; return sub { return unless @k; my $k = shift @k; return $k, $hr->{$k}; } } ;; my $e1 = my_each %hash; my $e2 = my_each %hash; ;; print qq{e1: @{[ $e1->() ]}; @{[ $e1->() ]}.}; print qq{e2: @{[ $e2->() ]} \n}; ;; print qq{e1: @{[ $e1->() ]}; @{[ $e1->() ]}.}; print qq{e2: @{[ $e2->() ]} \n}; ;; print qq{e1: @{[ $e1->() ]}; @{[ $e1->() ]}.}; print qq{e2: @{[ $e2->() ]} \n}; ;; print qq{e1: @{[ $e1->() ]}; @{[ $e1->() ]}.}; print qq{e2: @{[ $e2->() ]} \n}; " e1: three tres; five cinco. e2: three tres e1: one uno; two dos. e2: five cinco e1: four quatro; . e2: one uno e1: ; . e2: two dos
Update: Changed the code example slightly to clarify the termination behavior of the iterator.
In reply to Re^3: Indepedent lazy iterators for the same hash?
by AnomalousMonk
in thread Indepedent lazy iterators for the same hash?
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |