On Jul 7, Ryan Frantz said:
foreach my $MySortedFile (sort { $a->[1] <=> $b->[1]
or
$AlphaToNbr{lc($a->[2])} <=>
$AlphaToNbr{lc($b-
[2])} or
$a->[3] <=> $b->[3]
}
map {[$_, /^.(\d{4})(\w{3})(\d{2})/]}
<DATA> ) {
[snip]
}
1. Create a hash with numeric equivalents for the months.
2. Perform a sort by first comparing numbers (I'm assuming the YYYY, but
I don't quite know how that reference works).
3. Then comparing mmm (having been converted to lower case, the value of
the respective AlphaToNbr key is compared).
4. Compare another pair of numbers (DD?)
After that I'm lost. I'm not familiar with the 'map' function or what
happens after that.
You have to read it from the bottom up. FIRST the input filehandle is
read (in this case, DATA), and all the lines of input are fed to the map()
function. THEN the map() function returns a list of array references,
whose elements are: the original line, $1, $2, $3 (from the regex match).
This list of array references is then passed to sort(), which sorts them
first by their year, then by the hash-value associated with the lowercase
version of their month, and then by their day.
my %AlphaToNumber = (
jan => "1",
feb => "2",
mar => "3",
apr => "4",
may => "5",
jun => "6",
jul => "7",
aug => "8",
sep => "9",
"oct" => "10",
nov => "11",
dec => "12",
);
# sort chronologically using a code snippet from 'Wags'
@user_links = ( sort {
$a->[1] <=> $b->[1]
or
$AlphaToNumber{lc($a->[2])} <=> $AlphaToNumber{lc($b->[2])}
or
$a->[3] <=> $b->[3]
} @user_links;
map {[$_, /^.(\d{4})(\w{3})(\d{2})/]}
<DATA> );
Ok, you would do:
@user_links = sort {
$a->[1] <=> $b->[1]
or
$AlphaToNumber{lc($a->[2])} <=> $AlphaToNumber{lc($b->[2])}
or
$a->[3] <=> $b->[3]
} map {
[ $_, /^.(\d{4})(\w{3})(\d{2})/ ]
} @user_links;
Here, your @user_links array holds the data that Wags was reading from the
DATA filehandle. We still execute the map() on it, though, because we
need to get from "/2005Jun01-Jun04xxx" to the array reference containing
["/2005Jun01-Jun04xxx", 2005, 'Jun', '04'].
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>