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

Hi,
I have a follow-up question to one that I have previously asked.
How can I match at least a whole item in an array?
my @myTerms = ( 'blah foo', 'bar' ); my $term1 = "blah"; my $term2 = "blah foobar";
I now know that this will match $term1 to $myTerms[0]
( grep /\Q$term\E/, @myTerms)
but i would like to know the item in the array that matches $term2 and then use that item in place of $term2.

Is this possible?
Cheers,
Reagen

Replies are listed 'Best First'.
Re: "reverse" array match
by Roy Johnson (Monsignor) on May 27, 2004 at 14:45 UTC
    I'm a little unclear on the question, but taking my best guess about what you want:
    grep $term2=~/\Q$_\E/, @myTerms;

    The PerlMonk tr/// Advocate
      Oh of course. Sometimes I am blind to what is right in front of me. Thanks.
Re: "reverse" array match
by Happy-the-monk (Canon) on May 27, 2004 at 14:45 UTC

    traverse the array with a foreach loop and count, use   last   when you have found your match. Not as spiffy as a   grep   or   map,   but it might be even faster.