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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |