note
inman
The transliteration does not interpolate the variable as you expect in your sample. The following illustrates the use of eval.
<c>
use strict;
use warnings;
my @letters = qw (A B C D E F);
my $string = "AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF";
my $count;
foreach my $a(@letters) {
eval "\$count = \$string =~ tr/$a//";
print "$count\n";
}
</c>
You can also use a substitution in order to interpolate but avoid the eval.
<c>
$count = $string =~ s/$a//g;
</c>
Both methods change the string. A more flexible way to count the values would be to split the string and use a hash to store the values.
<c>
my %count;
$count{$_}++ foreach split //, $string;
print "$count{$_}\n" foreach (@letters);
</c>
526711
526711