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
Solved

keyword Matching

Posted on 2000-05-08
7
253 Views
Last Modified: 2010-03-05
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
Comment
Question by:shels
  • 4
  • 3
7 Comments
 

Author Comment

by:shels
ID: 2789041
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2789132
".....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
 

Author Comment

by:shels
ID: 2789191
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2789232
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
 

Author Comment

by:shels
ID: 2789243
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
 

Author Comment

by:shels
ID: 2789267
exactly what I was looking for, thanks man!!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2789300
most welcome :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
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…
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…

856 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