http://qs321.pair.com?node_id=1131550


in reply to Re^3: Need help figuring out how to order/eval the numbers
in thread Need help figuring out how to order/eval the numbers

for my $ita (sort { sort_italian() } keys %hash) { # loop code goes here }

Here's what the code above does.

  1. First, keys gets all the keys from the hash, in no particular order, and passes them to its left as a list.
  2. sort then sorts that list based on the comparison found in the subroutine sort_italian(), and passes the now-sorted list of keys to its left.
  3. 'for' points the variable $ita at the first value on that sorted list, and runs the code in the enclosing braces following it (where I've replaced the code with a comment above). After that block is finished, for points $ita at the second item on the list and runs the block again, then runs it with $ita pointed at the third item, and so on until the list is exhausted or a command inside the block stops it (like last, for instance).

See perlsyn for more info on the for (also known as foreach) operator. See the section "Foreach Loops" there; stay away from the C-style loops described in the "For Loops" section unless you know you need one.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.