How would you proceed to find the best match?

That depends somewhat on your definition of "best", and some more explanation and examples would help, but I'm going to guess it's the longest one?

for my $s (@search) { my @names = split ' ', $s; # this reqires at least first two names to match my @matches = grep { /^\Q$names[0]\E\s+\Q$names[1]\E\b/ } @source; @matches = sort {length($b)<=>length($a)} @matches; print "search='$s'\n"; print "\tfound='$_'\n" for @matches; } __END__ search='John Ronald Reuel T' found='John Ronald Reuel Tolkien' found='John Ronald S Tolkien' search='Trent Reznor' found='Trent Reznor' search='Barack Hussein II' found='Barack Hussein Obama II' found='Barack Hussein II' search='Barack Hussein Obama II' found='Barack Hussein Obama II' found='Barack Hussein II' search='No match here'

Just a note, using \w+ to match a name may not be enough, since it might not include all the characters you would consider part of a name (for example, in ASCII it doesn't include the dot, as in "Jr." or "Sr."). That's why the code above takes the alternative approach of splitting on whitespace. However, even that might not be enough, and you should probably look into the Lingua:: namespace on CPAN. For example, a quick search brings up Lingua::EN::MatchNames and Lingua::EN::NameParse.


In reply to Re: Searching for best match by Anonymous Monk
in thread Searching for best match by Sosi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.