in reply to Re: This should be easy: counting letters in a string
in thread This should be easy: counting letters in a string
Both methods change the string.No, tr/a// will not delete characters unless you specify the d modifier. Characters that are not replaced are left alone.
And using a little trick to induce list context, you can use pattern matching to count the occurrences:
my @letters = qw(A B C D E F); my $string = 'AFTYUBEWTWECRTUTYIYTDDDDRYJURTHJTREEEEEFGSDFF'; my @counts; for (0..$#letters) { $counts[$_] = () = $string =~ /$letters[$_]/g; } print "@counts\n";
|
---|