What's the safest way to iterate through the keys of a Perl hash?

后端 未结 9 944
情深已故
情深已故 2020-12-22 20:31

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in s

相关标签:
9条回答
  • 2020-12-22 20:52

    A few miscellaneous thoughts on this topic:

    1. There is nothing unsafe about any of the hash iterators themselves. What is unsafe is modifying the keys of a hash while you're iterating over it. (It's perfectly safe to modify the values.) The only potential side-effect I can think of is that values returns aliases which means that modifying them will modify the contents of the hash. This is by design but may not be what you want in some circumstances.
    2. John's accepted answer is good with one exception: the documentation is clear that it is not safe to add keys while iterating over a hash. It may work for some data sets but will fail for others depending on the hash order.
    3. As already noted, it is safe to delete the last key returned by each. This is not true for keys as each is an iterator while keys returns a list.
    0 讨论(0)
  • 2020-12-22 20:53

    I woudl say:

    1. Use whatever's easiest to read/understand for most people (so keys, usually, I'd argue)
    2. Use whatever you decide consistently throught the whole code base.

    This give 2 major advantages:

    1. It's easier to spot "common" code so you can re-factor into functions/methiods.
    2. It's easier for future developers to maintain.

    I don't think it's more expensive to use keys over each, so no need for two different constructs for the same thing in your code.

    0 讨论(0)
  • 2020-12-22 20:58

    The rule of thumb is to use the function most suited to your needs.

    If you just want the keys and do not plan to ever read any of the values, use keys():

    foreach my $key (keys %hash) { ... }
    

    If you just want the values, use values():

    foreach my $val (values %hash) { ... }
    

    If you need the keys and the values, use each():

    keys %hash; # reset the internal iterator so a prior each() doesn't affect the loop
    while(my($k, $v) = each %hash) { ... }
    

    If you plan to change the keys of the hash in any way except for deleting the current key during the iteration, then you must not use each(). For example, this code to create a new set of uppercase keys with doubled values works fine using keys():

    %h = (a => 1, b => 2);
    
    foreach my $k (keys %h)
    {
      $h{uc $k} = $h{$k} * 2;
    }
    

    producing the expected resulting hash:

    (a => 1, A => 2, b => 2, B => 4)
    

    But using each() to do the same thing:

    %h = (a => 1, b => 2);
    
    keys %h;
    while(my($k, $v) = each %h)
    {
      $h{uc $k} = $h{$k} * 2; # BAD IDEA!
    }
    

    produces incorrect results in hard-to-predict ways. For example:

    (a => 1, A => 2, b => 2, B => 8)
    

    This, however, is safe:

    keys %h;
    while(my($k, $v) = each %h)
    {
      if(...)
      {
        delete $h{$k}; # This is safe
      }
    }
    

    All of this is described in the perl documentation:

    % perldoc -f keys
    % perldoc -f each
    
    0 讨论(0)
  • 2020-12-22 20:58

    I may get bitten by this one but I think that it's personal preference. I can't find any reference in the docs to each() being different than keys() or values() (other than the obvious "they return different things" answer. In fact the docs state the use the same iterator and they all return actual list values instead of copies of them, and that modifying the hash while iterating over it using any call is bad.

    All that said, I almost always use keys() because to me it is usually more self documenting to access the key's value via the hash itself. I occasionally use values() when the value is a reference to a large structure and the key to the hash was already stored in the structure, at which point the key is redundant and I don't need it. I think I've used each() 2 times in 10 years of Perl programming and it was probably the wrong choice both times =)

    0 讨论(0)
  • 2020-12-22 21:06

    I usually use keys and I can't think of the last time I used or read a use of each.

    Don't forget about map, depending on what you're doing in the loop!

    map { print "$_ => $hash{$_}\n" } keys %hash;
    
    0 讨论(0)
  • 2020-12-22 21:08

    Using the each syntax will prevent the entire set of keys from being generated at once. This can be important if you're using a tie-ed hash to a database with millions of rows. You don't want to generate the entire list of keys all at once and exhaust your physical memory. In this case each serves as an iterator whereas keys actually generates the entire array before the loop starts.

    So, the only place "each" is of real use is when the hash is very large (compared to the memory available). That is only likely to happen when the hash itself doesn't live in memory itself unless you're programming a handheld data collection device or something with small memory.

    If memory is not an issue, usually the map or keys paradigm is the more prevelant and easier to read paradigm.

    0 讨论(0)
提交回复
热议问题