[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] Perl tips



On Mon, Nov 25, 2002 at 03:35:32PM +0000, Steve Tickle wrote:
> At 15:24 25/11/02 +0000, Alex Hudson wrote:
> Well the problem is that the filename contains a date and is in the form 
> something-dd-mm-yyyy. sort() works OK as long as the filename is 
> something-01-mm-yyyy to something-09-mm-yyyy. When it gets to 
> something-10-mm-yyyy it inserts the file between something-01-mm-yyyy and 
> something-02-mm-yyyy.

Usually the easiest thing is to build a hash of sort keys first:

    @files = ...
    foreach ( [at] files) {
	/-(\d\d)-(\d\d)-(\d\d\d\d)$/ or die "bad filename: $_\n";
	$dates{$_} = "$3$2$1";
    }
    @sorted_files = sort { $dates{$a} cmp $dates{$b} }  [at] files;


-- 
You never really learn to swear until you learn to drive.
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.