Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 wandering the Monastery: (9)
As of 2014-08-29 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (287 votes), past polls