Gossamer Forum
Home : General : Perl Programming :

pAttern matching

Quote Reply
pAttern matching
I need to compare the each inputed word to each words in the database per line.
You see i'm not good in pattern matching. Anyone?

Sample :


User inputed Keywords :

right one right now


database:

friend pet, body
magic, real
bad one body
right one right, now



output sample:


right one right now - 4 found
bad one body - 1 found


Quote Reply
Re: pAttern matching In reply to
If you want to match it exactly you would use......

open(FILE,"file.db") || die "Can't open file.db";
@file=<FILE>;
close(FILE);

foreach $line (@file) {
split(/,/,@file);
if ($line eq "wordsgohere") {
something();
}
else {
somethingelse();
}
}


Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: pAttern matching In reply to
Thanks for the reply... I got it.. i'll post if ever i need some assistance. thanks

Quote Reply
Re: pAttern matching In reply to
hi! I still need some help in my code.

our flat file db:

Waterfalls, mountain, atmosphere
Around the world, australia, site
Volcano, fire, around

when the user inputs the keyword "around" the script will definitely returned two output like this :

Around the world, australia, site
Volcano, fire, around

Can you guys help me on how I can remove that comma(,) in the output and I want the words before that comma),) will be stored into three separate variable coz i still need to print it together with the html.



--- the sample code---------
$pattern = "around";

@data = split(/\ /,$pattern);

open(FILE, "$datafile") || die "I can't open $datafile\n";
while ($line = <FILE>) {
$i = 0;

foreach $data (@data) {
if ($line =~ /$data\s/i) { $i++; }
}
if ($i > 0) {
$results{"$line"} = $i;
}
$i=0;
}

print sort { $results{$b} <=> $results{$a} } keys %results;


Quote Reply
Re: pAttern matching In reply to
Why don't you remove the spaces between the keywords, and then use the , as the delimeter? This would make it easier as you then don't need to get rid of the comma after you retrive the words...how does that sound?

Andy

http://www.ace-installer.com
webmaster@Ace-installer.com
Quote Reply
Re: pAttern matching In reply to
Why do you have this...

@data = split(/\ /,$pattern);

That means you are using a space as the delimiter?...Correct?

So as youradds says...remove that extra space and then you can split @data using the comma which will get rid of it when you print the result.



Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: pAttern matching In reply to
ok.. all CAPITALIZED $scalars are what I assume you can do yourself.

my @input = split /\s /, $USERINPUT; #splitting by \s allows you to allow user to input in a textfield (one word per line, or split by spaces)

my ($found, %results);
open DB, "<$DATABASE";
while (my $line = <DB>) {
$found = 0;
foreach my $input (@input) {
while ($line =~ m#$input#gi) { $found ; } # $foundPLUSPLUS; (board messed it up)
}
if ($found > 0) {
$results{$line} = $found;
}
}
close DB;

foreach (sort { $results{$b} <=> $results{$a} } keys %results) {
print (join " ", (split /\s*,\s*/)), " - $results{$_} found\n"; #not sure if i did this right
}

Jerry Su
widgetz sucks
Quote Reply
Re: pAttern matching In reply to
umm.. this wouldn't work actually..

your example was..
INPUT:
right one right now

DB:
right one right now
...

it would get that line in the database.. then search each of the words inputted in the line..

so it starts with: right.. it finds 2.. then it goes to: one.. finds 1.. then: right.. finds 2.. then: now.. finds 1..

in your example.. you said.. 4 found.. it finds 6

Jerry Su
widgetz sucks