eval '(exit $?0)' && eval 'exec perl -wST "$0" ${1+"$@"}' & eval 'exec perl -wST "$0" $argv:q' if 0; sub key_of_record { local($record) = @_; # Split record into lines: my @lines = split /\n/, $record; my ($i) = 1; my ($key) = $lines[$i]; while( !($key =~ m/^\\functionTitle\{(.*)\}/) && ($i < 5)) { $i=$i+1; $key = $lines[$i]; } return $key; } $/="\n\\end{function}"; # Records are separated by blank lines. @records = <>; # Read in whole file, one record per array element. @records = sort { key_of_record($a) cmp key_of_record($b) } @records; print @records;