I'm trying to search some arrays for the best possible matches. For instance, given the @source and @search lists:

my @source = ("John Ronald Reuel Tolkien","John Ronald S Tolkien","Tre +nt Reznor","Barack Hussein Obama II","Barack Hussein II"); #note that + the second item is wrong and should be discarded! my @search = ("John Ronald Reuel T","Trent Reznor","Barack Hussein II" +,"Barack Hussein Obama II","No match here");

I would like to associate the @search list with the best match in @source list. I'm figuring this can be done with a search pattern with several ORs, but I'm stuck. Please see my example below:

#!/usr/local/bin/perl use strict; use warnings; my @source = ("John Ronald Reuel Tolkien","John Ronald S Tolkien","Tre +nt Reznor","Barack Hussein Obama II","Barack Hussein II"); my @search = ("John Ronald Reuel T","Trent Reznor","Barack Hussein II" +,"Barack Hussein Obama II","No match here"); print "twonames\t\talternativesearch\n"; foreach my $s (@search){ #gets first two names (my $twonames=$s)=~s/^(\w+ \w+).*$/$1/; #gets all other names, if they exist (my $others=$s)=~s/^(\w+ \w+)//; if ($others){ #deletes initial space (my $alternativesearch=$others)=~s/^\s//; $alternativesearch=~s/\s/\|/g; print "$twonames\t\t$alternativesearch\n"; } else { print "$twonames\t\tNO OTHER NAMES PRESENT\n"; } } #prints # twonames alternativesearch # John Ronald Reuel|T # Trent Reznor NO OTHER NAMES PRESENT # Barack Hussein II # Barack Hussein Obama|II

In this search I would like to have an association between @search items and @source items that would yield the best match. Something like:

# search source # John Ronald Reuel T John Ronald Reuel Tolkien # Trent Reznor Trent Reznor # Barack Hussein Obama II Barack Hussein Obama II # No match here

Note that, in the case of Obama it matched the whole array, in the first line it matched the two first words plus something else, and in the last case it found nothing. How would you proceed to find the best match? (i.e. the match where the search sentence would be mostly contained in the source sentence?) Thanks

Edit: this was crossposted on StackOverflow

Edit2: Even though I used people's names in my example, my real case has no people's names in case that matters.


In reply to 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.