in reply to sorted Hash
Hey, let’s just make things a little easier for the next Gentle Reader, and actually explain things:
In the statement: sort { $a <=> $b } keys %hash)
... the part in bold underline is the Magick: an in-line sort comparison subroutine (which always takes two arguments/variables, always named $a and $b). This is used here in order that the numeric comparison operator (<=>) can be specified, instead of sort’s default, which is to use the string comparison operator (cmp).
If you glance at the output in the original post, you will see that the sort keys are “sorted correctly” ... as strings, not as numbers. (The “default” sorting behavior, in other words, is equivalent to: sort { $a cmp $b } ...)
Q.E.D.
P.S. This is one of the counter-intuitive features of Perl. Ordinarily, you might expect that there would be one comparison-operator which would “know” whether the values being passed are numeric or string. (As the ordinary “less-than, greater-than” relational operators do.) But this, actually, is a feature of the language that is specifically designed for use in sorting. The language, by default, consistently treats its sort-keys as strings, no matter what is their internal data-type at the moment, and yet makes it very easy ... as just demonstrated ... to compare them numerically.)