for my $ita (sort { sort_italian() } keys %hash)
{
# loop code goes here
}
Here's what the code above does.
- First, keys gets all the keys from the hash, in no particular order, and passes them to its left as a list.
- 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.
- '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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|