while (not eof DATA) { # Read four lines and store them in the @record array my @record = ( scalar(), scalar(), scalar() ); $c = $c + 3; # Remove the new-line from the end of the lines chomp @record; # Store a reference to this @record instance # in the @data array. $data[$record[0]] = [$record[1],$record[2]]; } # Arrays start at zero and the sort below will barf if you # don't have each element in the array be an array ref $data[0] = [] unless $data[0]; close DATA; my @sorted = sort { $a->[0] <=> $b->[0] } @data; print Dumper(@sorted)."\n"; for ($i = 0; $i < ($c); $i++) { next unless defined($sorted[$i]); for ($j = 0; $j <= 1; $j++) { print $sorted[$i][$j]."\n" if defined $sorted[$i][$j]; } }