Allegakoen, Justin Devanandan wrote:
-----8<---------
#build an array of our alphabet
@halfabet = qw/a b c d e f g h i j k l m n o p r s t u v z/;
-----8<---------
my @halfabet;
push @halfabet, $_ for 'a'..'z';
Nicer, and compiles under strict and warnings which Steve seems to have
left out.
Nicer...maybe, but working? Unfortunately not.
You've included q,w,x,y which were not part of the characters in
@halfabet. It wasn't just my bad typing, it was a part of the problem.
If the array was just a..z in alphabetic order there wouldn't have been
any point in creating @halfabet.
The sort would then break on the sequence of m..t because there is no q
in the users alphabet so it would detect a gap in the sequence and
switch to loading the tail. Then you wind up with r,s,t,m,n,o,p as the
chunks switched around.
I used a loop like yours when I was writing it originally but dropped it
because the direct array made it easier to focus on the sort rather than
the setup.
for (a..z){
next if /[qwxy]/;
push @halfabet, $_
}
The direct array assignment was simply less distracting. Doing it your
way it should look like...
push @halfabet, $_ for 'a'..'p','s'..'v','z';
But I think that's starting to get messy.
And the strict and warnings were not so much left out as removed after
I'd finished. My original did compile under strict and warnings. Again,
I was trying to keep it clear and uncluttered so the subtle part of the
script wouldn't be obscured. Strict and warned version below.
Steve
#!/usr/local/bin/perl
use strict;
use warnings;
my @files_b = qw/
file2s.ext file2a.ext file2t.ext
file2b.ext file2.ext file2u.ext
file2v.ext file2c.ext file2z.ext/;
#build an array of our alphabet
my @halfabet;
push @halfabet,$_ for 'a'..'p','r'..'v','z';
my %files;
my $base_file;
for(@files_b){
if(m/\d(.)\.ext$/){
$files{$1} = $_; #add files to a hash keyed by sequence letter
}else{
$base_file = $_; #set aside basefilename
}
}
my $tailing = 1; #flag so we know which list we're building
my @tail;
my @head;
for (@halfabet){ #iterate through the sort index
if ($tailing){
if(defined $files{$_}){ #ignore the unused letters
push @tail,$files{$_}; #add to the tail list a,b,c
}else{
$tailing = 0; #until we hit the end of the first list
}
}else{
if(defined $files{$_}){ #continue adding the remaining files
push @head,$files{$_}; # to the head list x,y,z
}
}
}
#reassemble the basename, head and tail lists base,x,y,z,a,b,c
my @sorted = ($base_file,@head,@tail);
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs