use warnings; use strict; my %hash; while (){ chomp; my ($key, $col2, $col3) = split; push @{ $hash{$key}{$col2} }, $col3; } for my $k (keys %hash){ for (keys %{ $hash{$k} }){ my $cols3 = join ':', @{ $hash{$k}{$_} }; print "$k $_ $cols3\n"; } }