[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Sheflug] Perl tips
On Mon, Nov 25, 2002 at 03:47:33PM +0000, Dave Mitchell wrote:
> 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;
Or, you can just write a function:
sub file2num {
my ($file) = @_;
return if ($file !~ /-(\d\d)-(\d\d)-(\d{4})$/);
return "$2$3$4";
}
my @list = sort { file2num($a) <=> file2num($b) } sort [at] files;
Depending on how long the list is, a function is usually a better idea,
since you could conceivably have a very long list :o)
BTW, I think you want the ufo operator in your example, like mine?
Cheers,
Alex.
___________________________________________________________________
Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html
GNU the choice of a complete generation.