The most serious problem with this code is the improper use of return in your subroutines. You aren't capturing the values your subroutines return. For example, if I want to get the return value of

sub double_each { return map {$_ * 2 } @_; }

I have to call it like this: @list_of_nums = double_each(@list_of_nums); (assuming, of course, I have numbers in @list_of_nums before I make the call. Currently, your calls to your search subs are simply throwing away the return values.

You're not using use strict here or running under -w (or with use warnings should you be using perl 5.6.0) as far as I can tell. If so, you would have been warned about using those arrays in "void context" (which is how Perl tells you you're throwing away the return values) But let's start with the biggest single thing: change those two lines before you create @matchlist and try the following:

my @descmatches = productsearchbydesc($pss); my @modelmatches = productsearchbymodel($pss);

That will get you part of the way.

Further advice: you've got two different subroutines that differ only in the array they search (otherwise the code is entirely interchangeable). Use that fact! Write ONE subroutine, and have it accept as arguments the search string AND a reference to the array that is to be searched:

sub searcharray { my ($terms, @array) = @_; ... my @matches = grep { $array[$_] =~ /$pattern/ } 0 .. $#array; @matches; } # call this with my @matches = searcharray($pss, @BKSIC_DESC);

Final note: it's much more efficient to pass around references to arrays and hashes than it is to pass around arrays and hashes. Read perlreftut for how to use references.

HTH.

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Conflicts combining arrays? by arturo
in thread Conflicts combining arrays? by akm2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.