Solved

counting occurence(s)

Posted on 2003-10-26
1
206 Views
Last Modified: 2010-03-04
I've got 2 variables named $one and $two.
$one contains a whole sentence, for example:

$one="The cat is never far from home";

$two contains some characters, for example:

$two="The e o";

Now I want to count the occurences of each character/word of $two in $one.
For example:
'The' should give as result 1, because the word The occures one time in $one
'e' should give as result 3, because the character 'e' occures three times in $one
'o' should give a result 2, because the character 'o' occures two times in $one

Which lines do I need for this ?
0
Comment
Question by:michael123199
1 Comment
 
LVL 20

Accepted Solution

by:
jmcg earned 25 total points
ID: 9625033
I would think you'd need two different approaches depending on whether the thing you were looking to count was a word or a character.

For characters, the tr/// operator is the one usually suggested. It returns a count of characters transliterated, even if you transliterate a particular letter into itself.

$lettercount = $one =~ tr/e/e/;

which places the value 4 in $lettercount (not 3 as you counted).

For words, there are two ways I can see to go. One is to use a regular expression match, possibly decorating the word to be found with \b delimiters to ensure that we do not find a word that appears embedded in another word (e.g. "pear" in "appears"). The other approach would be to split up the $one string and count how many times each word in it occurs (possibly arranging to lower-case words that are capitalized as we go along).

$counts{lc $_}++ foreach split /\W+/, $one;

Then, for the words in your list, the value of $counts{lc $word} can be directly accessed from the hash.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
execute pl/sql block from perl program 3 118
Perl for loop for 2000 ms 7 95
ppm conversion to curl on a module install 8 80
Perl Awk Need Help 3 109
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now