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

后端 未结 9 973
情深已故
情深已故 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 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;
    

提交回复
热议问题