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

Re: How to count substrings in an array?

by kcott (Abbot)
on Feb 03, 2013 at 07:37 UTC ( #1016794=note: print w/ replies, xml ) Need Help??


in reply to How to count substrings in an array?

You haven't really explained what you're ultimately trying to achieve here. If you put your question in terms of a concrete context, you'll probably get a better answer. Guidelines for doing this can be found here: How do I post a question effectively?

The following piece of code may do what you want or at least give you some idea of how to proceed.

$ perl -Mstrict -Mwarnings -E ' my @ary = qw{xyz xyx abc xxx def zyx}; my $sub = q{x}; my @counts = map { scalar @{[/$sub/g]} } grep { /$sub/ } @ary; say "All strings = ", scalar @ary; say "Strings with $sub in them = ", scalar @counts; say "Counts of $sub in strings containing $sub:"; say for @counts; ' All strings = 6 Strings with x in them = 4 Counts of x in strings containing x: 1 2 3 1

-- Ken


Comment on Re: How to count substrings in an array?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-09-20 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls