If you want to search two arrays and put the indices of the matching results from both into one array, you might need a way of keeping track of which array each stored index comes from (how do you know if this index corresponds to a match in @names, or in @desc, or in both?). Let's say you search @names first and @desc second, or you use OeufMayo's flattening approach: you might end up with the list (1,3,5,6,9,2,6,7). How do you know where the indices for @names leaves off and the indices of matching @desc entries begin? One answer is to return two lists (an array that consists of two anonymous arrays: the list of matches from @names, and the list of matches from @desc). Alternately, you could label which was which with a hash that consists of two anonymous arrays: so, with the above example, that hash would look like:

%matches = ( name=>[1, 3,5,6,9], desc=>[2,6,7] );
Either way, your search code wouldn't need to be modified much. Here's the hashy approach:
my %matches; $matches{name} = [ grep { $names[$_] =~/$pattern/ } 0 ..$#names ]; $matches{desc} = [ grep { $desc[$_] =~/$pattern/} 0.. $#desc];

And of course you would return \%matches instead of \@matches.

But it might be even simpler than this: if, as I suspect from your desire to remove duplicates, both arrays are part of a larger data structure (such that for each name, there is a corresponding description) then you can stick with the current approach, slightly modified. Simply change that grep line to grab the matches from either array:

my @matches = grep { $names[$_] =~ /$pattern/ || $desc[$_] =~/$pattern/ } 0 .. $#names;

which will also solve the problem of weeding out the duplicates.

HTH.

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


In reply to Re: Multi @array searches by arturo
in thread Multi @array searches 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.