my %hash; while (my $line = ) { chomp($line); my @columns = split ' ', $line; # Add an array of the first four columns to the hash # entry for this type (the type is $columns[5]). Each # hash entry is itself an array of these arrays. push @{$hash{$columns[5]}}, [ @columns[0..4] ]; } # Go through the type names in order, printing out all the # data that was seen with each type foreach my $type (sort keys %hash) { print "$type\n"; foreach my $line (@{$hash{$type}}) { print "@$line\n"; } }