SysAdm has asked for the wisdom of the Perl Monks concerning the following question:

Search problem: I have several variables in a data file... I have a search function that looks at one of the variables and then checks two other searches in another variable if the first one is present... Another words... my data file has $class, $date and $desc columns... I need to do a search on the $class variable and check it against two different search criteria's in the second variale... Here's the code for the search problem:
<form action="$ENV{'SCRIPT_NAME'}" method="POST"> <input type="hidden" name="action" value="search"> <input type="text" name="searchstring" size="18"> <select name="searchprice" size="1" value="price"> <select name="searchtype" value="keyword" size="1"> if($class =~ /$form{'searchtype'}/ eq 'keyword' and ($desc =~ /$form{' +searchstring'}/i) and ($desc =~ /$form{'searchprice'}/) || ($desc =~ +/$form{'searchstring'}/i) and ($desc =~ /$form{'searchprice'}/)) {print "$desc\n";
Let me know if you need more of the code... I basically need to compare (3) different events before printing the $desc. ______SysAdm

Replies are listed 'Best First'.
Re: Search question
by dfog (Scribe) on Mar 11, 2001 at 08:42 UTC
    I've found that the easiest way to create comparisons using boolean algebra is to start with them overly simplistic and then slowly bring it together in it's tightest form.

    For your search, I would start with
    if ($form{searchtype} eq "keyword") { #check that what was filled in +the form was right if ($class =~ /keyword/) { #Check the class is type keyword if ($desc =~ /$form{'searchstring'}/i) { #Make sure the string + is in $desc if ($desc =~ /$form{'searchprice'}/) { #Check for price in + $desc print "$desc\n"; } } } }

    Because it is broken down like this, it is easy to see repeats in logic, and what conditionals might be missing. It also eases debugging of the logic, since you can put print statements to see a particular conditional is not working. Now we can use each nested if as an "and" and rewrite it as:
    if (($form{'searchtype'} eq "keyword") && ($class =~ /keyword/) && ($desc =~ /$form{'searchstring'}/i) && ($desc =~ /$form{'search +price'}/)) { print "$desc\n"; }

    From your question, the above should print desc only when those 4 different conditions are all met.
    HTH
    Dave
      Thanks Dave... worked great... My next problem is to search the amount field for dollar amounts.... I need to be able to search for dollar amounts in ranges... ie ($1,000 or less) ($1,000 - $1,500) etc, etc.. here's my code for the dollar amount to search and it doesn't seem to work right..
      <select name="searchprice" size="1" value="price"> <option value="\$" selected>Any Price <option value="<1000">Under \$1,000 <option value="1000-1500">\$1,000 - \$1,500 <option value="1501-2500">\$1,500 - \$2,500 <option value="2501-5000">\$2,500 - \$5,000 <option value="5001-10000">\$5,000 - \$10,000 <option value="10001-15000">\$10,000 - \$15,000 <option value="15001-999999">\$15,000 - UP </select></font></td></tr> if ($desc =~ /$form{'searchprice'}/) {print "$desc\n";}
      My data looks like this:
      
      bla bla bla bla $1,000
      bla bla $12,500. bla bla
      bla $5000 bla bla bla
      
      sometimes it has a comma sometimes it doesn't...
      sometimes it has a period directly after it and so on...
      
      ____SysAdm
      
        To match that I would do something like :
        my ($LowValue, $HighValue) = split("-", $form{'searchprice'}); #makes + the two values to compare $Price with if ($desc =~ m/\$(\d{1,3},?[\d{3},?]*\.?\d{0,2})/) { my ($Price) = $1; #sets $Price equal to the match in the regex $Price =~ s/,//g; #Strips out all commas if (($LowValue <= $Price) && ($Price <= $HighValue)) { print "$desc\n"; } } else { print "No price found in $desc\n"; }
        The regular expression is assuming that all of the prices in the description will begin with a $, and that they are properly formatted in terms of money (no misplaced commas and such). I believe this will do what you are looking for.

        Dave