#! perl use strict; use warnings; use Data::Dump; my %hash; while () { push @{ $hash{$2} }, $1 if / ( ([^\s_]+) _ [^\s-]+ - \S+ ) /x; } print "\nData structure (HoA):\n"; dd \%hash; print "\nOutput:\n"; for (sort keys %hash) { my $array_ref = $hash{$_}; print $_, ' ', scalar @$array_ref, ' ', join(' ', @$array_ref), "\n"; } __DATA__ foo_1-a foo_2-b foo_3-b foo_4-b bar_1-a bar_2-a bar_3-b bar_4-a bar_5-b