Setting aside the way Perl handles this specific case, ask yourself how a computer program could keep track of which items in a collection it had already processed, if items can be inserted into the middle of the collection during processing. The only way I know is to build a second list. There are two ways to do this: you can keep a list of ones you've already done, or a list of ones you haven't done yet. If your keys (in this case, the hash keys) have a defined order, you can in theory limit your list of not-done-yet ones somewhat by having "all the ones with a key greater than this value n" be one (possibly implicit) always-final entry in the list, adding entries to the list only when that doesn't cover them, and raising the value of n whenever it's the only item in the list. This requires some work on your part, though, and in most cases is probably not worth it. If your footprint is a significant issue, though, it is an option. Something along the lines of this...

$n = minimum_key(\%h); # minimum_key left as an exercise while (@n or ($n<maximum_key(\%h))) { # also maximum_key if not (@n) { push @n, keys_between(\%h, $n, $n+1); # Yep, keys_between is also left as an exercise. $n++; } my $thiselement = shift @n; process_element($thiselement); }

If you do something like this, be sure to be consistent with your inequalities (e.g., if an element is equal to n, it either has to be in @n or covered by $n but not both). Also of course if you insert anything into the hash as part of the processing you have to test whether the key is less than $n and if so add it to @n.


for(unpack("C*",'GGGG?GGGG?O__\?WccW?{GCw?Wcc{?Wcc~?Wcc{?~cc' .'W?')){$j=$_-63;++$a;for$p(0..7){$h[$p][$a]=$j%2;$j/=2}}for$ p(0..7){for$a(1..45){$_=($h[$p-1][$a])?'#':' ';print}print$/}

In reply to Re: foreach loop question by jonadab
in thread foreach loop question by dda

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.