[ic] mv_results and perl

Kevin Walsh interchange-users@icdevgroup.org
Wed Dec 18 12:40:01 2002


Brian Kosick [briank@nacs.net] wrote:
> 
> I'm trying to refine search results manually with perl.  From what I've 
> gathered during archive searches, $Search->{'mv_results'} is where the 
> search results are.  WHat I would like to do is find all results that 
> match 0 in a column, and delete those from the array and pass it back so 
> that the new mv_results can be used farther on down the results page.
> So I've written the following code into the results page, before the 
> [search-region] tag.
> 
> [perl table="products"]
> my $s = $Search->{''};
> return "No search passed." if ! $s;
> my $ary = $s->{mv_results};
>   return "Bad search object." if ! $ary;
>   return "No match." unless @$ary;
>   my $out = 'Removed: ';
>   foreach $row (@$ary) {
>         my ($sku) = @$row;
>         # Get a field you didn't return in the search
>         $price = tag_data(products, dc_price, $sku);
>         if($price == 0)
>         {
>                 delete $ary{$row};
>         }
>   }
>   $Search->{'mv_results'} = $ary;
>   return;
> [/perl]
> 
> This always gives me nothing or something like ARRAY(KJHDF#$(&*)
> I would also like to incorprate a counter decrementor for 
> Search->{'matches'} so that the number is decremented for each item removed.
> 
Using your [perl] block as a template, here's an (untested) block of
code that should remove the '1234' SKU from the resultset:

    [calcn]
        my $s = $Search->{''};
        return "No search passed." unless $s;

        my $ary = $s->{mv_results};
        return "Bad search object." unless $ary;
        return "No match." unless @$ary;

        for my $row (0 .. $#$ary) {
            my $sku = $ary->[$row]->[0];
            splice(@$ary,$row,1) if $sku eq '1234';
        }
        undef;
    [/calcn]

You can modify that code to remove items based upon the price, or
whatever you need.

Note the following:

  1. The use of 'splice' instead of your attempt to delete a hash
     key out of an array.

  2. The loop will fail if the SKU is not the first column in the
     resultset.

  3. Any failure will result in messages, such as 'Bad search object',
     appearing on the web page, instead of in the error log.  Your
     users may or may not be kind enough to forward the messages to
     you.

>
> I had originally tried using
> [item-next][calc][item-price] eq "0"[/calc][/item-next]
> But it didn't decrement the match number so on a search that originally 
> returned 25 matches of which say 7 were skipped.  The more list would 
> still display "Matches 1-25"
> 
You are modifying the resultset after the match count has been
calculated.  You will have to decrement the match count yourself.

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin@cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/