use strict; use warnings; my $str = "aabceezazerrfde"; my %wanted = (a => 7, e => 2, z => 26); my $tot = 0; my $counter = 0; $tot += $_ for map {++$counter; $wanted{$_}} grep {exists $wanted{$_}} split //, $str; print "Found $counter letters totaling $tot\n"; #### Found 9 letters totaling 81