# $n contains field to sort on # records to sort are in @ARGV my @records; while (<>) { # chomp deleted so we don't have to add \n later push @records, [ split ',' ]; } # now sort them; @records = sort { $$a[$n] cmp $$b[$n] } @records; # now print them foreach $record (@records) { print join (',',@$record); }