Solved

counting occurence(s)

Posted on 2003-10-26
1
204 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

11 Experts available now in Live!

Get 1:1 Help Now