#!/usr/bin/perl use strict; use warnings; if (!-d "sorted") { mkdir "sorted" or die "unable to create dir sorted $!"; } my @files2sort = ; #just use glob to get names my $curfilenum =1; foreach my $file (@files2sort) { open my $fh_in, '<', $file or die "$file failed to open $!"; open(my $fh_out, '>', "./sorted/$file.sort") or die "cannot create out $file.sort $!"; print "Processing ".$curfilenum++." of ".@files2sort." $file\n"; sortfile($fh_in, $fh_out); close ($fh_out); close ($fh_in); print "OK: Sorted $file \n"; } sub sortfile { my ($fh_in, $fh_out) = @_; my @lines = <$fh_in>; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my ($x) = $_ =~ /VerNumber:\((\d+)/i; [$_, $x]; } @lines; print $fh_out @sorted; } __END__ Processing 1 of 2 file1.txt OK: Sorted file1.txt Processing 2 of 2 file2.txt OK: Sorted file2.txt #these names are confusing... my $files_size = $#files + 1; my $index_file = 1; #better names?: my $num_files = @files; # the value of an array in a scalar context is the # number of items in the array, no need for $#files+1 # or you can just use @files in a scalar context without # creatng $num_files at all. my $file_counter =1; # "$index_file" would mean something different