Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The Rule: reset the iterator if and only if other elements are deleted.
I don't think a new iterator is the best generic approach.
Why? What's wrong with getting a new iterator as long as the function is documented to reset it?

I'm really not a fan of your proposition, because it makes it look like this is a normal iteration over an hash, but you only know otherwise by looking inside the block. It's kind of obscure as well, you might iterate normally for a few keys, and in some conditions start over, (which is kind of confusing when iterating over an hash with it's sometimes random order). Also by having the loop logic split in two places, you can't abstract it away with:

while (my $key = $remaining_keys->next()) { ...; }
where you might decide that you finally do care about the order of traversal, and just make next() call .get_most_important_key() instead of .get_random_key() or .get_first_key().

I guess you're trying to solve the problem of iterating over a hash while changing it's content, but not always removing the current key. This would do the work:

while (my ($key, $value) = delete_first %hash) { do_stuff(\%hash, $key, $value); $subset{$key} = $value if valid($key); }
does the work, and at least there's no back and forth in the way you iterate over the hash.

ot sure how costly resetting is in the end.
Doesn't matter, I'm resetting in the beginning :D

In reply to Re^7: Iterating over hash while deleting elements (Best Practice) by Eily
in thread Iterating over an hash while removing keys by Eily

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2020-07-12 11:49 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found