my @strings = qw( aaabbbb ab abb aabb aaabb aabbb ); for my $string ( @strings ) { my $a_counter = $string =~ tr/a/a/; my $b_counter = $string =~ tr/b/b/; print "In $string there were $a_counter 'a's and $b_counter 'b's. \n"; }