=head1 Documentation for sort.pl 20001205 - Program originally written by Chad Kulesa 20001205 - Program modified and documented by Larry Boatman Purpose: Passed up to four parameters, the first two of which (input and output file names) are required (sort key offset from beginning of record and length of sort key are optional), this "generic" sort program reads in the input file (name passed in the first parameter), performs the sort (see further documentation below) , and writes out the output file (name passed as second parameter). =cut =pod Declare an "assignable" array as the "output" array =cut my @lines2; =pod if less than or more than the number of arguments then display a "usage" message exit program Open the input file or report why not and exit Read entire file into an "input" array Close the input file Sort the "input" array into the "output" array based on the comparison of sort keys built depending upon the number of arguments passed to the program as follows: if no sort key offset and no sort key length is passed, then sort on entire record else if a sort key offset is passed but no sort key length, then sort on entire record beginning from the sort key offset else if both a sort key offset and sort key length is passed, then sort on key beginning at sort key offset for sort key length end if end if end if Open the output file Write the sorted "output" array to the output file Close the output file =cut if (($#ARGV < 1) || ($#ARGV > 3)) { print "usage: $0 input.filename output.filename [sort key offset] [sort key length]\n\n"; exit; } open (INPUT,"$ARGV[0]") || die "Cannot open $ARGV[0] for reading: $!"; my @lines = ; close INPUT; if ($#ARGV == 1){ @lines2 = sort {$a cmp $b} @lines; } elsif ($#ARGV == 2){ @lines2 = sort {substr($a, $ARGV[2]) cmp substr($b, $ARGV[2])} @lines; } elsif ($#ARGV == 3){ @lines2 = sort {substr($a, $ARGV[2], $ARGV[3]) cmp substr($b, $ARGV[2], $ARGV[3])} @lines; } open (OUTPUT,">$ARGV[1]") || die "Cannot open $ARGV[1] for writing: $!"; print OUTPUT @lines2; close OUTPUT;