Gossamer Forum
Home : Products : Links 2.0 : Customization :

Help with this array

Quote Reply
Help with this array
I'm wanting to assign some synonyms for certain search terms and this is what I've started to do:


# assign term synonyms
@synonyms = (employment , jobs);

if ($in{'query'} eq @synonyms[1]) {
($in{'query'} = @synonyms[0])
};

I placed this in sub main of the search.cgi script.

It works great if someone just searches for jobs. But how can I get it to work if someone searches for "technical jobs"?
Quote Reply
Re: Help with this array In reply to
You can try:


# assign term synonyms
@synonyms = (employment , jobs);


if ($in{'query'} =~ @synonyms[1]) {
($in{'query'} =~ s/@synonyms[1]/@synonyms[0]/g)
};


I haven't tried it, but it should work. I added the g to make sure all instances of the word are found in the string.

Dan O.


After some further thinking, using a hash (associative array) might be a lot faster.
Try:

# assign term synonyms to a hash
%synonyms = ("jobs" , "employment"
"helper", "assistant",
"gopher", "assistant",
"presedent", "management");

foreach $word (keys %synonyms){
($in{'query'} =~ s/$word/$synonyms{"$word"}/g)
};

Again, I haven't tried it but should work, barring any logic errors I may have had. >8~}

Be sure to watch for the curly braces '}' in $synonyms{"$word"} they're not brackets ')'. And yes the '$' is correct when used that way.

Dan O.


Please let me know if they work.






[This message has been edited by DanO (edited October 24, 1999).]
Quote Reply
Re: Help with this array In reply to
The first one worked...the second one didn't.

Grady

[This message has been edited by Grady (edited October 31, 1999).]