• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

keyword Matching

I want to know how to match each word  a user types in against each keyword in an array.  Then to output the amount of times a word was successfully matched to one of the keywords!
0
shels
Asked:
shels
  • 4
  • 3
1 Solution
 
shelsAuthor Commented:
This is part of the code I'm already using but it does not produce the desired output.  I want it just to output one line saying the amount of times keywords were found, but this code outputs a line for each keyword matched.
Any ideas on how I should ammend the code or maybe a better way to do it??

while ($string =~ /(\S+)/g) {  # groups of non-whitespace characters
    if (exists $word{$1}) {      # $1 is that group of characters
      $word{$1}++;               # increment the times it was found
    }
  }
print "$word{$_} asthma keyword(s) were found in the heading.\n";
for (@aswordlist) {
    if ($word{$_}) {  # if there was at least 1 match for the word
    print "$word{$_} asthma keyword(s) were found in the heading.\n";
    }
  }
0
 
maneshrCommented:
".....but this code outputs a line for each keyword matched. "

as per your code this is how it is supposed to work.

i think the @aswordlist array has the list of your keywords. Now you are using the foreach loop (viz. for (@aswordlist) { )and going thru EVERY element of that array!!

if you want to print specific keyword, then you will have to first check if the keyword you are looking for is indeed the one that is being processed currently in the loop.

here are 2 examples.

the first shows all elements of the array.

@aswordlist=("allergy","nasal","flu","flonase");
foreach(@aswordlist){
  print $_,"\n";
}


The foll shows ONLY what you asked for.
$what="nasal";

@aswordlist=("allergy","nasal","flu","flonase");
foreach(@aswordlist){
  print $_,"\n" if /^$what$/;
}

So the above will ONLY print nasal, since that is string you are looking for.

So basically in the code you have posted above, you need to use a variable similar to $what and print the results ONLY when a match is found.

Hope that helps.
0
 
shelsAuthor Commented:
Thanks a mil. that does help me a bit, but what I actually want to output is the number of times keywords were found in the text, not any one keyword.  
At the moment my output for text containing three different keywords, looks like this:
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.

but I want the output to look like the foll:

3 asthma keyword(s) were found in the heading

Thanks again for your help!
0
Independent Software Vendors: 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!

 
maneshrCommented:
from the looks of it, the fix might be very simple.

try this...

## variable that keeps a total of all the keywords found!!
$total_count=0;
for (@aswordlist) {
  if ($word{$_}) {  # if there was at least 1 match for the word
    $total_count++;
  }
}

## NOW, print the results..
print "$total_count asthma keyword(s) were found in the heading.\n";
0
 
shelsAuthor Commented:
Thanks a mil. that does help me a bit, but what I actually want to output is the number of times keywords were found in the text, not any one keyword.  
At the moment my output for text containing three different keywords, looks like this:
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.
1 asthma keyword(s) were found in the heading.

but I want the output to look like the foll:

3 asthma keyword(s) were found in the heading

Thanks again for your help!
0
 
shelsAuthor Commented:
exactly what I was looking for, thanks man!!!
0
 
maneshrCommented:
most welcome :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now