use strict; use warnings; my $str = "aabceezazerrfde"; my %wanted = (a => 7, e => 2, z => 26); my $tot = 0; $wanted{$_} && ($tot += $wanted{$_}) for (split //, $str); print "Total $tot\n";