Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'+' crashing search script ?!

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
Heres my script:

$entry{'searchbox'} = "+1";

open(FILE,"SparkPlugPrices.db");
@lines = <FILE>; close(FILE);
foreach $line (@lines) {
chomp($line);
($P,$D,$PR,$MU,$VatCode,$Weight) = split(/\|/,$line);
$LineCount++;
$PartNumber[$LineCount] = $P;
$PartDesc[$LineCount] = $D;
$Price[$LineCount] = ($PR * $MU / 100 + $PR);
$Price[$LineCount] = sprintf("%.2f\n", $Price[$LineCount]);
}
$entry{'searchbox'} =~ tr/a-z/A-Z/;

print "Content-type: text/html\n\n";

## Search Loop ##

if (($PartDesc[$lineNo] =~/$entry{'searchbox'}/) || ($PartNumber[$lineNo] =~ /$entry{'searchbox'}/)){

print <<EndOf;
<table border="0" cellpadding="2" cellspacing="2" style="border-collapse: collapse" bordercolor="#FFFFFF" width="850" id="AutoNumber4" bgcolor="#FFFFFF">
<tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'">
<td width="200" align="center" bordercolor="#000000"><font size="2">$PartNumber[$lineNo]</td>
<td width="450" align="center" bordercolor="#000000"><font size="2">$PartDesc[$lineNo]</td>
<td width="100" align="center" bordercolor="#000000"><font size="2">£$Price[$lineNo]</td>
<td width="100" align="center" bordercolor="#000000">
<form style="margin-bottom:0;" name="OrderLine$Result" method="POST" action=" <input type="hidden" name="Item" value="$PartNumber[$lineNo]">
<input type="text" name="qty" size="4" value="1" onkeyPress="return fnCheckNumeric();" onblur="this.value=removeSpaces(this.value); this.value=numbersOnly(this.value);">&nbsp;<input type="submit" value="Add" name="Add">
</form>
</td>
</tr>
</table>
EndOf
}
}


Basically it works with $entry{'searchbox'}, only if it doesn't have a '+' sign in fron of it!

Any ideas?
 
+" is a special character in regular expressions, which you're using in your search. If you want an exact text match (rather than using a proper regular expression), do this:
Code:
if ( $partDesc[$lineNo] =~ /\Q$entry{searchbox}\E/ ) {
  # etc etc
}
 
that works fine if the searchbox value is found ... if its not the script crashes still .. problem i have is some part numbers in my database are:

+1
..
+10
WR7DC+
...
++23
..
bp+7e
.. etc

How do i generate a working search script to stop the + symbols making it crash .. ?
 
Even if for example 'searchbox' contains, for example +8 .. but it returns no result .. ?
 
You need to escape the meaningful regex characters in $entry{searchbox}. Or possibly replace them with dots, so that +8 becomes .8, with the . matching any character.

This might increase the number of false positives you find in the search, but at least it would work...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Surely part numbers should be exact matches, rather than checking if the string the user enters is contained somewhere in the part number, no?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top