## UNTESTED use Data::Dumper; my %cats; foreach my $cat_word (keys %words) { my ($cat,$word) = split(/-/,$cat_word); push(@{$cats{$cat}},$word); } print Dumper \%cats;