Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

counting occurence(s)

Posted on 2003-10-26
1
Medium Priority
?
231 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

886 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