... 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.