Just another Perl shrine | |
PerlMonks |
Re^4: Speeding up stalled scriptby Athanasius (Archbishop) |
on Feb 05, 2015 at 13:53 UTC ( [id://1115664]=note: print w/replies, xml ) | Need Help?? |
Hello choroba, Yes, you’re right: despite the calls to delete, the for loop list doesn’t change, so it must be constructed before the first loop iteration. Whereas using each I get an infinite loop:
Output:
Which isn’t surprising given the warning in the documentation for each: If you add or delete a hash's elements while iterating over it, the effect on the iterator is unspecified; for example, entries may be skipped or duplicated--so don't do that. Exception: It is always safe to delete the item most recently returned by each()... So it appears that sundialsvc4’s concern over memory is justified: for a large hash, the use of keys in a for loop will consume significantly more memory than the equivalent use of each. It seems strange to me that the documentation doesn’t highlight this — or did I just miss it somewhere? Cheers,
In Section
Seekers of Perl Wisdom
|
|