Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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;
    --
    <http://www.dave.org.uk>

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-01-26 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (69 votes). Check out past polls.

    Notices?