#!/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"; sortfile2($fh_in, $fh_out); close ($fh_out); close ($fh_in); print "OK: Sorted $file \n"; } sub sortfile2 { my ($fh_in, $fh_out) = @_; my @lines = <$fh_in>; @lines = sort by_version @lines; print $fh_out @lines; #can do a sort "in place" #separate @sorted var is not needed. } sub by_version { my ($verA) = $a =~ /VerNumber:\((\d+)/i; my ($verB) = $b =~ /VerNumber:\((\d+)/i; $verA <=> $verB #returns -1,0,+1 } __END__ Processing 1 of 2 file1.txt OK: Sorted file1.txt Processing 2 of 2 file2.txt OK: Sorted file2.txt