sub letter_counts { my @letters = split//, lc(shift); my %counts; for(@letters){ $counts{$_}++; } return %counts } my $word1 = 'bantha fodder'; my $word2 = 'banana slug'; my %counts1 = letter_counts($word1); my %counts2 = letter_counts($word2); my $result = 'are equivalent'; test_loop: for(keys %counts1, keys %counts2){ if(!defined $counts2{$_} || !defined $counts1{$_}){ $result = 'are not equivalent'; last test_loop } } print "'$word1' and '$word2' $result\n";