in reply to @arrys & Non exact searches.

I've used this trick before (even though it goes against advice I just gave on another question ... well, the situation's different here, so ...)

Once you've split the search terms up, you might try joining 'em back together with a pipe | which will allow you to write a simple regex to match any of your search terms.

I'm going to assume that each element of @names is a full name. What you want, I take it, is a list of the names which match your disjunctive search (you're doing this by updating a global variable ... it's much cleaner to return the list of matches from the sub, that way lies less confusion; this routine returns a list of matches; as written, it also expects to be passed what the user entered in the search form.)

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


Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
don't get bit by split.
by danger (Priest) on Mar 21, 2001 at 00:11 UTC

    I just want to point out the potential bug that is almost always lurking around uses of split /\s+/. If there is leading whitespace in the string, that code will give you a null leading element in the return list. In the case of arturo's search routine above, if the search term passed in happens to contain a leading space (for whatever reason), then the pattern constructed will look like /|term1|term2|etc/ and will match on any string (and this bug might be difficult to spot -- even if you print out the pattern string you might not notice the leading | in the pattern).

    So, the moral is, usually when you want to split on multiple whitespace you'll want to use the special case of just a string with a single space in it as the first argument to split(), ie: split " ", $terms;. (and split() with no arguments is just doing: split(" ",$_)).

    A second point about your search subroutine is that you can use the construct the pattern with the case sensitive switch embedded in the pattern via (?i). You can also then use the qr// operator so that the regex does not have to be recompiled for each name passed through the grep block. So, I'd change that routine to:

    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; }

      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.

        Instead of this:
        my @matches = grep { /$pattern/ } @names;
        this will get just the indexes:
        my @matches = grep { $names[$_] =~ /$pattern/ } 0..$#names;

        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