sort files by extension
From: Perl.Org (perl.org_at_jpw3.com)
Date: 07/28/04
- Next message: James Edward Gray II: "Re: sort files by extension"
- Previous message: Ramprasad A Padmanabhan: "Re: reading log files in real time"
- Next in thread: James Edward Gray II: "Re: sort files by extension"
- Reply: James Edward Gray II: "Re: sort files by extension"
- Reply: Jeff 'Japhy' Pinyan: "Re: sort files by extension"
- Maybe reply: WilliamGunther_at_aol.com: "Re: sort files by extension"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Wed, 28 Jul 2004 13:27:53 -0400
I have a list of files I want to case-insensitive sort by extension, files
with no extension appearing first. It should handle both Windows and Unix
directory separators. I think I have working code, but I am interested in the
various syntax for this one - there MUST be a better way than my feeble attempt:
use strict;
my @input = ( '/path/to/file/with.ext', '/path/to/file/with.htm',
'/path/to/file/without', '/path/to/file/with.eml', '/path/to/file/with.pdf' );
my @output = sort
{
my $ex1 = '';
my $ex2 = '';
if ( $a =~ m#[^\\/]\.([^\\/]+)$# )
{
$ex1 = $1;
}
if ( $b =~ m#[^\\/]\.([^\\/]+)$# )
{
$ex2 = $1;
}
return( lc( $ex1 ) cmp lc( $ex2 ));
} @input;
print join( $/, @output );
C:\temp>sortext.pl
/path/to/file/without
/path/to/file/with.eml
/path/to/file/with.ext
/path/to/file/with.htm
/path/to/file/with.pdf
- Next message: James Edward Gray II: "Re: sort files by extension"
- Previous message: Ramprasad A Padmanabhan: "Re: reading log files in real time"
- Next in thread: James Edward Gray II: "Re: sort files by extension"
- Reply: James Edward Gray II: "Re: sort files by extension"
- Reply: Jeff 'Japhy' Pinyan: "Re: sort files by extension"
- Maybe reply: WilliamGunther_at_aol.com: "Re: sort files by extension"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|