http://qs1969.pair.com?node_id=66069


in reply to don't get bit by split.
in thread @arrys & Non exact searches.

I've ask about about this before, I know (sorry but I need to do it again). I don't understand how to rewrite the code to store only the index number of the matching @names elements in </code>@matches</code>.

below is the current code.

sub search { my $terms = shift; my $pattern = join "|", split " ", $terms; my $case = $FORM{case} eq 'insensitive'?"(?i)":""; $pattern = qr/$case$pattern/; my @matches = grep { /$pattern/ } @names; return \@matches; }

Can somebody educate me, please.

Replies are listed 'Best First'.
Re: Re: don't get bit by split.
by runrig (Abbot) on Mar 21, 2001 at 21:43 UTC
    Instead of this:
    my @matches = grep { /$pattern/ } @names;
    this will get just the indexes:
    my @matches = grep { $names[$_] =~ /$pattern/ } 0..$#names;
Re: Re: don't get bit by split.
by davorg (Chancellor) on Mar 21, 2001 at 21:51 UTC

    Replace your grep line with this:

    my @matches = grep { $names[$i] =~ /$pattern/ } 0 .. $#names;
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me