in reply to Re: Advanced Sorting
in thread Advanced Sorting

I am just getting into perl,and have a basic simple question. I have written a script that reads in a directory of logfiles, sorts them in reverse chronological order, and outputs them into an html page. I cannot figure out how to sort the directory deeper than just the month, which comes first (the files in the dir are all listed by month/day/year). How can I sort this so the most recent days are first?

Replies are listed 'Best First'.
RE: RE: Re: Advanced Sorting
by little (Curate) on Oct 28, 2000 at 19:57 UTC
    1. read the perlman (or unixman) for the commands stat() and sort()
    2. try to understand the following :-)
    sub bydate{(stat $dir.$a)[9]<=>(stat $dir.$b)[9];} @sortedfiles = sort bydate @files_in_your_dir;
    Have a nice day
    All decision is left to your taste
    Update
    ok, I might have been wrong when assuming that logs get analyzed but not changed, after they have been archived
        #!/usr/local/bin/perl use strict; $|++; my $dir = "../../logs/"; my $output = "../stats/current.txt"; #read the dir and put all files to a list opendir DIR, $dir || die "blah!"; my @allfiles = readdir DIR; close DIR; # remove those files from list that don't seem to be logfiles my @logs = grep /^\d{6}.?\.log$/i, @allfiles; # sort in a way you like my @sorted = map{ $_->[0] } sort {$a->[1] cmp $b->[1]} map { [ substr($_,2,2).substr($_,0,2).substr($_,4)] } @logs; # start output open OUTPUT, ">$output" || die "blah!"; print OUTPUT join "\n", @logs; close OUTPUT; # end output