I have the array:
@letters = (A,B,C,D,E,F);
and the string:
$string = "AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF";
and I want to find how many times each letter appears in the $string.
I go like:
foreach $a(@letters)
{
$count = ($string = ~tr/$a//);
print $count;
}
But it doesn't count anything...
Does it have to do with the $a in the pattern matching?
I tested the same thing and instead of $a I put A or B etc and it works...
foreach $a (@letters)
{
$count = ($string =~ s/$a/$a/g);
print "count of $a = $count\n";
}
or you can use the map function too:
foreach $a(@letters)
{
$count = map{m/$a/g} $string;
print "count of $a = $count\n";
}