akm2 has asked for the wisdom of the Perl Monks concerning the following question:

This probably a very simple question. Sorry, but Im having a mental block.

I've got a fixed record length DB I substr into @arrys. One array contains names of people. I need a user to type in their name and I need to find it. If someone keys in Jeff Fox and I have A Jeff Patrick Fox III I want it displayed. The code I was trying to accomplish this is:

sub search { $matchcounter = 0; @terms = split(/\s+/, $FORM{'terms'}); foreach (@names) { $string = join(' ',@names); $string =~ s/\n//g; foreach $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if (!($string =~ /$term/i)) { $include{$matchcounter} = 'no'; last; } else { $include{$matchcounter} = 'yes'; } } } } }

Replies are listed 'Best First'.
How to match names imprecisely in strings.
by boo_radley (Parson) on Mar 20, 2001 at 21:17 UTC
    Well, I'd take a look at Lingua-EN-MatchNames, first off.
    from the description :

    You have two databases of person records that need to be synchronized or matched up, but they use different keys--maybe one uses SSN and the other uses employee id. The only fields you have to match on are first and last name. That's what this module is for. Just feed the first and last names to the C<name_eq()> function, and it returns C<undef> for no possible match, and a percentage of certainty (rank) otherwise. The ranking system isn't very scientific, and gender isn't considered, though it probably should be.
    It's got some good examples, and you can set up "fuzzy searching", so you can match, e.g. "G. W. Bush, Jr" to "George Bush Jr" with a degree of confidence.
    and if that doesn't work, I'd try Text::soundex or just grepping through the strings.
Re: @arrys & Non exact searches.
by arturo (Vicar) on Mar 20, 2001 at 21:30 UTC

    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

      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.

Re: @arrys & Non exact searches.
by tadman (Prior) on Mar 20, 2001 at 21:27 UTC
    Implementing Google or AltaVista in Perl for this kind of application is likely overkill, so here's a simple solution.
    my (@names) = ( ... ); sub search { my (@search_keywords) = split (/\s+/, shift); my ($match_code) = "sub{".join('&&', map { "/\Q$_\E/" } @searc +h_keywords)."}"; my ($match_func) = eval $match_code; return grep { &$match_func() } (@names); } my (@results) = search ("the fried potato king");
    This will return all records that match all terms. For a version that matches any, switch the '&&' in the join() to be '||' instead.

    Essentially, this 'search' function generates a subroutine that tells grep() if the array entry matches or not. You can have a look at the value of $match_code to see what it is doing.

    The \Q and \E are used to escape the metacharacters which would interfere with the // regexp. Saves you from having to tr/// them yourself.