Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: don't get bit by split.

by akm2 (Scribe)
on Mar 21, 2001 at 21:40 UTC ( #66069=note: print w/replies, xml ) Need Help??

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;

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://66069]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2023-03-27 13:08 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.