Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^4: Need help figuring out how to order/eval the numbers

by aaron_baugher (Curate)
on Jun 23, 2015 at 00:11 UTC ( #1131550=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1131550]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-05-30 06:41 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (171 votes). Check out past polls.