Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: This should be easy: counting letters in a string

by Roy Johnson (Monsignor)
on Jan 31, 2006 at 14:46 UTC ( #526760=note: print w/ replies, xml ) Need Help??


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";

Caution: Contents may have been coded under pressure.


Comment on Re^2: This should be easy: counting letters in a string
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526760]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (16)
As of 2015-07-01 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls