viewing paste file_sorter | Perl

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/perl
if(scalar(@ARGV) < 1){
        print "Please enter filenames\n";
        my $sFilename = <>; chop($sFilename);
        @afilename = split(' ',$sFilename);
}
else { @afilename = @ARGV; }
print "Filenames given = @afilename \n";
foreach my $sFile (@afilename){
        unless(-r "$sFile"){ print "Can't read/found $sFile\n"; next; }
        open FILEHANDLE,"$sFile"  or next;
        print "Starting to sort $sFile\n";
        my @aAllline = <FILEHANDLE>;
        foreach(@aAllline){    
                if(length($_)<3 || $_ =~ /\/\//) { next; }     
                push(@aLine,$_);
        }
        my @aSortedline = sort _sortById(@aLine);
        unless(-w "$sFile"){ print "Can't writte $sFile.sorted \n"; next; }
        open FILEHANDLE2,">$sFile.sorted" or next;
        my $sI = 0;
        foreach(@aAllline){
                if(length($_)<3 || $_ =~ /\/\//) { print FILEHANDLE2 $_; }
                else { print FILEHANDLE2 $aSortedline[$sI]; }
                $sI++;
        }
}
print "End code\n";
 
sub _sortById {
        my @aAline = split('#',$a);
        my @aBline = split('#',$b);
#       print "a=$a, b=$b\n aline = @aAline \n bline = @aBline \n";
        return (($aAline[0] <=> $aBline[0]) or ($aAline[0] cmp $aBline[0]) ); #cmp only ID
}
Viewed 600 times, submitted by lighta.