CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Counting matches with an array of strings

 



smilebey
Novice

Jan 2, 2013, 8:48 AM

Post #1 of 9 (1883 views)
Counting matches with an array of strings Can't Post

Hello guys,

I am trying to count matchings for 50 different words in a string and I want 50 numbers (number of matching) as an output.

So far I am searching for one word ($string) in $filing and the result is the number of matchings for this particular word ($num1):

Code
my $string="california"; 

my $num1=0;
$num1++ while ($filing =~ m/$string/g);


How can I extend the code to 50 different strings (maybe array) and the output is an array of 50 integers?

Thanks in advance for any help

Best Regards
smilebey


Laurent_R
Veteran / Moderator

Jan 2, 2013, 9:50 AM

Post #2 of 9 (1880 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post

You could use two arrays, one for storing the strings and one for storing the counters.

Or use a more elaborate data structure such as an array of arrays, in which each individual array would contain two information: the string and the number of occurrence.


Rahul6990
Novice

Jan 2, 2013, 11:05 AM

Post #3 of 9 (1876 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post

This will work for any number of strings:

$string= "hello abc how is abc this is def looking for ghi and also want to see ijk with def";
@string = split(' ',$string);
@InputString = ("abc","def","ghi","ijk","ooo");
foreach $StringInput(@string)
{
foreach $input(@InputString)
{
if($StringInput =~ m/$input/)
{
$StringCount{$input} ++;
}
$StringCount{$input} = 0 if(!exists($StringCount{$input}));
}

}
foreach ( keys %StringCount)
{
print "\n String : $_ - Count : $StringCount{$_}";
}

CheersWink
Rahul


FishMonger
Veteran / Moderator

Jan 2, 2013, 11:39 AM

Post #4 of 9 (1869 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post


Code
use strict; 
use warnings;
use Data::Dumper;

my @states = qw(California Navada Arizona Texas);
my $pattern = join '|', @states;
my $trip = "To get from California to Texas I drove though Navada then back to California before going through Arizona";

my %states;
$states{$1}++ while $trip =~ /($pattern)/g;

print Dumper \%states;



BillKSmith
Veteran

Jan 2, 2013, 1:12 PM

Post #5 of 9 (1860 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post

Any of the methods suggested will 'work' the same as your original post. Are you sure that your regular expression agrees with your idea of word. It will find a match in the following examples.

"Alaskan crab are the best"

"My recent gift was a new yorkshire terrier."

"I remained at work."

You may encounter another problem which we probably cannot solve. The meaning of the words is not always the name of the state.

"I bought a new jersey at Old Navy."

"They named the twin girls "Georgia" and "Virginia".
Good Luck,
Bill


smilebey
Novice

Jan 2, 2013, 2:39 PM

Post #6 of 9 (1854 views)
Re: [BillKSmith] Counting matches with an array of strings [In reply to] Can't Post

Thank you all for the great suggestions. I combined your solutions and coded mine. But the concern by Bill might be a problem. Dont know how severe.

Is there any way to go through the string from word to word? Split is a solution but that would take too much computing time since my strings are the content of long text files.


smilebey
Novice

Jan 3, 2013, 7:05 AM

Post #7 of 9 (1823 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post

Found it. Thanks anyways.
Sorry, but I am a beginner in Perl and RegEx.


BillKSmith
Veteran

Jan 3, 2013, 11:54 AM

Post #8 of 9 (1819 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post

I wrote my comments to show that you had not posted sufficiently detailed requirements. Your answer indicates that you do not know them. You can get in serious trouble with your boss when your program does not do what he expects. Right now is the time for you to develop a formal test suite and get your company to accept it. Refer to perldoc Test::Tutorial for help in getting started. Most of your tests should be unit tests of the regular expression. Ask for help in correcting the errors that you find.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Jan 3, 2013, 3:24 PM

Post #9 of 9 (1814 views)
Re: [smilebey] Counting matches with an array of strings [In reply to] Can't Post


In Reply To
But the concern by Bill might be a problem. Dont know how severe.

Is there any way to go through the string from word to word? Split is a solution but that would take too much computing time since my strings are the content of long text files.


Although some of the examples given by Bill can be solved by technical improvements, the general issue raised by Bill goes far beyond technical solutions. The problem is that the bill of requirements is unsufficiently detailed. If my client (or my boss, or whoever) asks me to recognize the word Virginia in a text file, I will happily do it, but don't ask me to recognize whether is it an American State or the first name of my elder daughter. No way to know, unless you can give me a context condition, a business rule, or some other form of litmus test to find out.

(To tell the full truth, my daughter's first name is really Carolina, not Virginia. That makes the test somewhat easier, just look for "North" or "South" before the word Carolina if it is a State of the Union or my daughter's name, but that leaves the Virginia or Georgia issue, as well as many others, unsolved.)


(This post was edited by Laurent_R on Jan 3, 2013, 3:36 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives