Where you have:
Having "almost" the same SQL code in 2 branches of an if is very hard to maintain or debug correctly, in my opinion.my $onlyonce = 1; foreach my $tempword ( @words ) { if ( $onlyonce == 1 ) { $searchstring .= "MajorDescription LIKE '%$tempword%' OR M +ajorName LIKE '%$tempword%'"; $onlyonce = 0; } else { $searchstring .= " OR MajorDescription LIKE '%$tempword%' +OR MajorName LIKE '%$tempword% +'"; } }
Why not replace all of that with:
join and map sometimes scare folks, but I think that's more readable and less fragile than your structure.$searchstring=join " OR ", map "MajorDescription LIKE '\%$_\%' OR MajorName LIK +E '\%$_\%'", @words;
If you don't like join/map, may I suggest this alternative?
my $or=""; foreach my $tempword ( @words ) { $searchstring .= " $or MajorDescription LIKE '%$tempword%' OR +MajorName LIKE '%$tempword%'"; # only set this once $or="OR" unless $or; }
In reply to Re: Search engine code critique
by RMGir
in thread Search engine code critique
by ruhk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |