Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi all!
My question is rather silly, but I can't seem to be able to find the solution myself...
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...
Code tags added by GrandFather
2006-01-31 Retitled by g0n, as per Monastery guidelines
Original title: 'This should be easy...'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: This should be easy: counting letters in a string
by GrandFather (Saint) on Jan 31, 2006 at 11:56 UTC | |
|
Re: This should be easy: counting letters in a string
by prasadbabu (Prior) on Jan 31, 2006 at 11:44 UTC | |
|
Re: This should be easy: counting letters in a string
by inman (Curate) on Jan 31, 2006 at 11:36 UTC | |
by Roy Johnson (Monsignor) on Jan 31, 2006 at 14:46 UTC | |
|
Re: This should be easy: counting letters in a string
by gube (Parson) on Jan 31, 2006 at 12:01 UTC | |
|
Re: This should be easy: counting letters in a string
by myuji (Acolyte) on Jan 31, 2006 at 12:48 UTC | |
|
Re: This should be easy: counting letters in a string
by McDarren (Abbot) on Jan 31, 2006 at 14:21 UTC | |
|
Re: This should be easy: counting letters in a string
by Anonymous Monk on Jan 31, 2006 at 13:10 UTC |