Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: don't get bit by split.

by akm2 (Scribe)
on Mar 21, 2001 at 21:40 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found