Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://526760]
help
Chatterbox?
[Discipulus]: msgd daxim about his post is almost empty: in 15 minutes i'll mark for reaping

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-11-22 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (319 votes). Check out past polls.

    Notices?