Solved

counting occurence(s)

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 159
Is it possible to get a piece of info in a line from a big text file with perl? 7 70
Extract data from span tag 1 93
Perl string filter 5 80
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.…
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…

831 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