Quantcast
Channel: sakuramboo.com
Viewing all articles
Browse latest Browse all 10

Wildcard Search Strings

$
0
0

I came across this while updating my companies web based sales tool. I had to create lookup files for all of our sales files. The format of the files are as follows.

(order number):(account name):(sales rep):(ad start date)

We have an advanced search feature where you can search for specific criteria such as all order from a particular rep or of a specific account name. The issue I had was when dealing with the ad start date. To fix this issue, I had to rewrite all dates into a YYYYMMDD format. This helped, but the real problem was that the user did not have to input all information. How I figured out how to do this was pretty slick, if I say so myself.

I initially created two different methods to achieve this search function. The first method was to take each search criteria and search each line of each lookup file. If the string was in it, it would push the line to an array. And then every other search criteria would then push those lines from that array into another array.

I think you can see the problem with that one.

The next method I came up with was to to find the search criteria from each line and each selection would have its own array and then find all similar lines in each array and pass that to another array. Again, way too much work for something so simple.

If we look at the actual string, this is what we are looking for.

(.*):(.*):(.*):(.*)

Since .* is a wild card for any character any number of times, any time that a selection is left blank (or, in this case, had “Any” selected), the section of the string would be (.*):. This is where I figured out that the search criteria should make a search string and try to find any line that matches that search string. This way, we are just reading the lookup files once and instead of passing the matched lines into an array, we just print it to the page. This is the bit of code that does all of this.

foreach $num (reverse 4000..$prefix){
     open(BOOK, "$location/$num-list.txt") or die $!;
     foreach $line (<BOOK>){
          $searchfor = "(.*):";
          ($io, $account, $salesrep, $startingdate) = split(/:/, $line);
          if($acctname eq "All"){
               $searchfor = $searchfor . "(.*):";
               } else {
               $searchfor = $searchfor . "$acctname:";
               }
          if($repname eq "All"){
               $searchfor = $searchfor . "(.*):";
               } else {
               $searchfor = $searchfor . "$repname:";
               }
          if($startdate eq "00000000" and $enddate eq "00000000"){
               $searchfor = $searchfor . "(.*)";
               } elsif($startdate le $startingdate and $enddate ge $startingdate){
               $searchfor = $searchfor . "$startingdate";
               } else {
               $searchfor = $searchfor . "00000000";
               }
          if($line =~ $searchfor){
               print qq(<tr><td><a href=io-edit.cgi?iovar=$io&user=$user&type=$type&id=$id target="_blank">$io</a> </td></tr><td></td><td>$account </td><td></td><td></td><td></td><td>$salesrep </td><td></td><td></td><td></td><td>$startingdate</td></tr><br \/>);
                }
          $startingdate = "";
          }
     close(BOOK);
     }

Simplicity is really much easier than it looks.

Now, there are some bugs in here, but nothing that can’t be fixed rather quickly.

The thing with this method is, each section between the colon’s have a wildcard by default. Since the first section does not have a value, it automatically starts with (.*): and each “Any” value gets (.*): appended to it. This makes comparing each line easy since we are looking for characters between the colon’s.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images