Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a file with the following data
1-22-03 somedata_somedata 1-23-03 wwwwwwwwwww 1-22-03 sssssssssssss 2-3-03 aaaaaaaaaaaa 1-3-03 sdddddd 2-3-03 ddddddddddddd 1-22-03 eeeeeeeeeee 2-3-03 hhhhhhhhh 1-3-03 kkkkkkkkkkkkkkk 2-3-03 llllllllllllllll
I need to produce a list sorted by the date . As you can see, there may be more than one entry for a date.
Can anyone provide a simple method of doing that?
Thanks!

Replies are listed 'Best First'.
Re: File - sort by date (hash, array?)
by broquaint (Abbot) on Sep 18, 2003 at 11:07 UTC
    If the file is small enough, just slurp it in and compare the first column by munging the date into ISO format
    open( my $fh, '<', $your_file ) or die "ack: $!"; my @data = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my($m,$d,$y) = /^(\d+)-(\d+)-(\d+)/; [ $_, sprintf "20%d%02d%02d", $y, $m, $d ] } <$fh>; print @data; __output__ 1-3-03 sdddddd 1-3-03 kkkkkkkkkkkkkkk 1-22-03 somedata_somedata 1-22-03 sssssssssssss 1-22-03 eeeeeeeeeee 1-23-03 wwwwwwwwwww 2-3-03 aaaaaaaaaaaa 2-3-03 hhhhhhhhh 2-3-03 ddddddddddddd 2-3-03 llllllllllllllll
    The above just applies the ever-handy Schwartzian transform to the first column and sorts the data by date.
    HTH

    _________
    broquaint

Re: File - sort by date (hash, array?)
by liz (Monsignor) on Sep 18, 2003 at 11:11 UTC
    sub n { $_[0] =~ m#^(\d+)-(\d+)-(\d+)(.*)#; sprintf( "%02d%02d%02d%s",$3,$1,$2,$4 ); } my @line = sort {n($a) cmp n($b)} <DATA>; print @line; __DATA__ 1-22-03 somedata_somedata 1-23-03 wwwwwwwwwww 1-22-03 sssssssssssss 2-3-03 aaaaaaaaaaaa 1-3-03 sdddddd 2-3-03 ddddddddddddd 1-22-03 eeeeeeeeeee 2-3-03 hhhhhhhhh 1-3-03 kkkkkkkkkkkkkkk 2-3-03 llllllllllllllll
    Not the fastest way to do this, as the normalization routine is called multiple times for each element. But it should do the trick, I think.

    Liz

    Update:
    Broquaint's solution calls the normalization only once for each element, and is thus less CPU intensive. But will use a lot more memory because of the the intermediate array that is built. YMMV.

Re: File - sort by date (Golf!)
by BrowserUk (Patriarch) on Sep 18, 2003 at 11:27 UTC

    Updated!

    perl -e"print substr$_,6 for sort map{sprintf('%02d'x3).$_,/^(\d+)-(\d ++)-(\d+)/} <>" junk

    broquaint pointed out that in my enthusiasm for golfing this I forgot to reorder the date components.

    P:\test>perl CON junk print substr$_,6 for sort map{/^(\d+)-(\d+)-(\d+)/ &&sprintf('%02d'x3).$_,$3,$1,$2}<> ^Z

    Not so good, but at least it works this time! Someone can do better than that I'm sure.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: File - sort by date (hash, array?)
by qmole (Beadle) on Sep 18, 2003 at 13:20 UTC
    There's More Than One Way To Cheat :-)

    perl -MDate::Parse -e 'print substr$_,10 for sort map{/ /;str2time($`).$_}<>'