perl.org <[EMAIL PROTECTED]> wrote:

: Thanks for the detailed response.  I know the interspersed
: comments won't make some members of the list happy, but
: they're just opinions.
: 
: James Edward Gray II wrote:
: : Not a "fan" of map() and grep() or just don't understand them?
: 
: Both, didn't understand them and still find them hard to
: read.  I have to worry about programmers with no Perl
: experience maintaining the code, and since these structures
: don't seem to exist in any other languages they are
: intimidating.  Plus, the Perl may need to get ported to
: Java or C#, which is easiest if the logic structures can
: be similar.  I would only use them if they significantly
: improve performance, not to reduce typing (since then I
: would have to type a long comment reminding me what the
: logic does - your explanation of this solution is precise
: but lengthy).

    There is another solution to this problem that involves
only simple perl statements that should be easier to read
than map and ?:. It is based on some slides by MJ Dominus
about an Indirect Sort.

 http://perl.plover.com/yak/hw2/samples/slide003.html


use File::Basename 'fileparse';

my @files = qw(
    /path/to/file/index.ext
    /path/to/another/file/with.htm
    /path/to/YA/file/small
    /path/to/file/foo.eml
    /path/to/file/bar.pdf
    /index.htm
);

# create an array of extensions
my @extensions;
foreach my $file ( @files ) {
    push @extensions, ( fileparse( $file, '\..*' ) )[2];
}

# sort the indices
my @sorted_indices =
    sort { $extensions[$a] cmp $extensions[$b] }
    0 .. $#extensions;

my @sorted_files = @[EMAIL PROTECTED];


    I used File::Basename's fileparse() subroutine to
get the file extensions. You could expand this for
clarity.

my @extensions;
foreach my $file ( @files ) {
    my( $base, $path, $type ) =  fileparse( $file, '\..*' );
    push @extensions, $type;
}


    Note that you could easily create additional
arrays for other sorts.

# create an array of extensions and filenames
my( @extensions, @filenames );
foreach my $file ( @files ) {
    my( $base, $path, $type ) =  fileparse( $file, '\..*' );
    push @extensions, $type;
    push @filenames, "$base.$type";
}

# sort indices by extension
my @extension_sorted_indices =
    sort { $extensions[$a] cmp $extensions[$b] }
    0 .. $#extensions;

# sort indices by filename
my @filename_sorted_indices =
    sort { $filenames[$a] cmp $filenames[$b] }
    0 .. $#filenames;

my @extension_sorted_files = @[EMAIL PROTECTED];
my @filename_sorted_files  = @[EMAIL PROTECTED];


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328









-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to