Solved

counting occurence(s)

Posted on 2003-10-26
1
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

626 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