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:
Searching

 



stanley
Novice

Apr 21, 2001, 9:02 AM

Post #1 of 7 (826 views)
Searching Can't Post

I have a flat database:

========================================
free stuff;free game|http://www.test.com/freestuff.html
free|http://www.test.com/
========================================
When someone query the word "free stuff" it will goes to free stuff with url http://www.test.com/freestuff.html

While somone query "free anything" will goes to url http://www.test.com

Currently, my problem it's whenever someone query "free stuff" it will goes to url http://www.test.com instead of http://www.test.com/freestuff.html, it seem likes the query only matching the last line, any solution?





Jasmine
Administrator / Moderator

Apr 21, 2001, 1:21 PM

Post #2 of 7 (823 views)
Re: Searching [In reply to] Can't Post

Care to post your search code so we can see what's going on?

Please enclose your code in

Code
 and

(without the extra spaces after the [)



stanley
Novice

Apr 21, 2001, 1:51 PM

Post #3 of 7 (821 views)
Re: Searching [In reply to] Can't Post


Code
open (file, "$file") || &open_error("$file"); 
@lines = <file>;
close (file);

foreach $rec (@lines){
chomp($rec);
($world,$url)=split(/\s*\|\s*/,$rec);
@ph=split(/\;/,$word);
foreach $ph (@ph) {
@words = split(/\;/,$ph);
foreach $aword (@words) {
if ($query=~ /$ph/i) {
$load=$url;
}
} }

Thanks.




stanley
Novice

Apr 22, 2001, 10:39 AM

Post #4 of 7 (803 views)
Re: Searching [In reply to] Can't Post

if I change the text file structure to

========================================
free|http://www.test.com/
free stuff;free game|http://www.test.com/freestuff.html
========================================

it's fine...can anyone help?





Jean
User


Apr 22, 2001, 11:34 PM

Post #5 of 7 (795 views)
Re: Searching [In reply to] Can't Post

Hi Stanley,

Your main problem is that =~ matching is interpreted as includes and not as equals,
i.e. free stuff will match free as well as free stuff.
If you want an exact match you'll have to use the eq.

Next one is I can't see the use for the following lines,
IMO you can safely remove them (together with the closing curly)

Code
@words = split(/\;/,$ph);   
foreach $aword (@words) {

Regards,

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


stanley
Novice

Apr 24, 2001, 7:33 AM

Post #6 of 7 (773 views)
Re: Searching [In reply to] Can't Post

That mean i have to use "eq" instead of "includes" for exact matching?

It's that any possibilities that I can use the "includes" because I would like to catch the phrase, not just the keywords.

Please advice,

Thanks



BigRich
Novice

Apr 25, 2001, 2:11 AM

Post #7 of 7 (758 views)
Re: Searching [In reply to] Can't Post

Below is an example of how you can try to match the keywords of the query exactly or, if they don't match exactly, assign a near match.


Code
#!/usr/bin/perl -w 

use strict; # always use strict!

my $query = "Free";

#default url in case there are no matches
my $default = 'http://www.test.com/';

# @near_match for close, but not exact, matches
# $load, url of the match
my (@near_match, $load);

open(FILE, $file) || &open_error("$file");
# should uncomment below to lock it unless it's read-only
# or flock is not supported (win32)
#flock(FILE, 2) || &open_error("Could not lock $file");

# better to iterate over the contents of $file than
# slurp it all in with @something = <FILE>;
while (<FILE>) {
chomp;
my ($phrase, $url) = split /\|/; #separate keywords from url
my @keywords = split /;/, lc($phrase); #split keywords for url
foreach (@keywords) {
if (lc($query) eq $_) {
#exact match, assign url to $load and exit the loop
$load = $url; last;
}
elsif (/$query/i) {
# close but not exact, put url in @near_match and try again
push @near_match, $url;
}
}
}
close(FILE);

# If $load wasn't assigned to an exact match, assign a random url
# from the @near_match array. If there were no near matches, assign
# the default url to $load
unless ($load) {
$load = (@near_match) ? $near_match[rand(++$#near_match)] : $default;
}

print "\$load = $load";

BigRich


 
 


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

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