OK.
let's assume that %inv looks like this :
(Apple=>10, Peach=>5, Berry=>9, Cobbler=>3, Cherry=>7 )
Now that that's out of the way, let's break things down.
foreach $invnumctr ( sort{$inv{$a} <=> $inv{$b}} keys %inv) { the first thing to worry about is the keys statement. that yields an array :
(Apple, Peach, Berry, Cobbler, Cherry)

so now the sort takes over -- looking the items from the list above, two at a time, putting one into $a and the other into $b, and using them for hash keys. This might look like :
$inv{Apple} <=> $inv{Peach} $inv{Peach} <=> $inv{Berry} $inv{Peach} <=> $inv{Cobbler} ...
and so on. The important thing to remember is that it's still using just the keys to sort on! By the time all is said and done, sort knows that Cobbler's value is less than Peach's value is less than Cherry's value is less than Berry's value is less than Apple's value (no puns, please), and that's what gets passed to foreach. see sort for the full perldoc version, and perlop for the scoop on the spaceship operator.

In reply to Re: sort keys question (boo) by boo_radley
in thread sort keys question by treebeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.