Ryan Frantz wrote: >> Here is one way to approach: >> !perl >> >> use strict; >> use warnings; >> >> my %AlphaToNbr = qw(jan 1 feb 2 mar 3 apr 4 may 5 jun 6 jul 7 aug 8 >> sep 9 oct 10 nov 11 dec 12); 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> ) { >> chomp($MySortedFile->[0]); >> print $MySortedFile->[0] . "\n"; >> } >> > > I have a rudimentary understanding of the above now (from reading Ch > 15 of 'Learning Perl' and the perlreftut manpage). This is what I > grok (assuming a YYYYmmmDD format): > > 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). The (\d{4}) implies numeric and there MUST be four digits(the form can be {lowest char count,highest character count). If you leave blank like {1,} then says 1 to as many as you can find. > 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?) Correct if the year is equal and the month is equal then compare the days. > > After that I'm lost. I'm not familiar with the 'map' function or what > happens after that. > > Also, you use an imaginary scalar that would contain the data. I have > the data in an array and tried to use your sortsub as follows (taking > some cues from the LP book); I think I'm off base... > > ----begin snip---- > (omitted creation of array @user_links) > 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> );
Just take <DATA> and replace that with @user_links. The code should look like: @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; The map is creating an array reference where item 0 is Capturing the whole input line item 1 is the (\d{4}) Capturing the year item 2 is the (\w{3}) Capturing the month item 3 is the (\d{2}) Capturing the day Now as you view the sort you see the $a->[1] <=> $b->[1] which is comparing the year then [2] is comparing the numeric month then [3] is comparing the numeric day Wags ;) > ----end snip---- > >> __DATA__ >> /2005Jul01-2005Jul02/foo/bar.html >> /2005Jul05-2005Jul06/foo/bar.html >> /2005Jun09-2005Jun10/foo/bar.html >> /2005Jun10-2005Jun11/foo/bar.html >> /2005Jun13-2005Jun14/foo/bar.html >> /2005Jun14-2005Jun15/foo/bar.html >> >> Output: >> /2005Jun09-2005Jun10/foo/bar.html >> /2005Jun10-2005Jun11/foo/bar.html >> /2005Jun13-2005Jun14/foo/bar.html >> /2005Jun14-2005Jun15/foo/bar.html >> /2005Jul01-2005Jul02/foo/bar.html >> /2005Jul05-2005Jul06/foo/bar.html ******************************************************* This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ******************************************************* -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>