I'm walking through the examples and exercises in "Effective Perl Programming" by Joseph N. Hall and Randal Schwartz (2 asides: 1) yeah,
Merlyn and, 2) reading on my Kindle, where the formatting is inconsistent and some fairly odd typos mar the text; looks like it was translated to Kindle format by someone unfamiliar with Perl).
This script, pure ASCII -- or so I think, is adapted from an example on sorting:
#!/usr/bin/perl
use Modern::Perl;
say "\n\n\t sort on multiple keys";
my @first = qw(John Joe John Jon Fred Rich);
my @last = qw(Smith Smith Jones Jackson Jones Adler);
my @index = sort {
$last[$a] cmp $last[$b]
or
$first[$a] cmp $last[$b]
} 0 .. $#first;
for (@index) {
say "$last[$_], $first[$_]";
}
say "\n \t But, testing, single sort (cmp):";
my @sorted = sort { $a cmp $b } qw(John Joe John Jon Fred Rich);
for (@sorted) {
say "\t $_";
}
=head execution
sort on multiple keys
Adler, Rich
Jackson, Jon
Jones, John
Jones, Fred
Smith, John
Smith, Joe
But, testing, single sort (cmp):
Fred
Joe
John
John
Jon
Rich
## sort puts "John" before "Joe" in the first instance but not in the
+second: Why?
=cut
Can someone point me to the document I failed to read or the section I failed to read carefully enough that explains the disparity in the output?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.