Given an unsorted list of URLs (ie from a DBI query) and a target URL to match ... I need to find the longest Matching URL in the unsorted list ... and the portion of the Target URL that isn't matched.

For example if the list contains:
http://www.someplace.com/foo
http://www.someplace.com/foo/bar
http://www.someplace.else.com/foo

and the target is:
http://www.someplace.com/foo/fighter/index.html

it should return:
http://www.someplace.com/foo, fighter/index.html

I have the following code (generalized for this post):

sub getMapping = { my $uri_in = shift; $uri_in =~ m!.*://([^/]*)!; my $domain = $1; my $dbh = getDBH(); my $path = ''; my $query = qq{ SELECT ftpID, uri, path # first get all records that FROM tURIMapping # refer to the domain WHERE uri REGEXP '.*$domain.*'; }; my $sth = $dbh->prepare($query); $sth->execute; my $aMapping = undef; # now let's search for the best match my $i = '0'; while ($aMapping = $sth->fetchrow_arrayref()){ # while there are still mappings my $target = $uri_in; # set the target while ($i < 5) { last if $aMapping->[1] eq $target; # exit if this is the mapping $target =~ s!(^\w+://.*)/(.*)$!$1!i; # remove the last part of th +e uri $path = '/'.$2.$path if $2; # save the last part as the path last if $target =~ m!^\w+://[^/]/?$!i; # exit if this just a doma +in $i++; } # end while ($i) last if $aMapping->[1] eq $target; # double check this is the targe +t } # end while ($aMapping = $sth->fetchrow_arrayref()) die "No Mapping Found for $uri_in" unless $aMapping; my ($ftpID, $uri, $map_path) = @$aMapping; # if the ftpID is null my $type = $ftpID? 'ftpAccount':'filesystem'; # this is a filesystem $sth->finish; $dbh->disconnect; return ($type, $map_path.$path, $ftpID); };
The code did come at a point where I was just beginning to write perl after a long haitus so I apologize for the "unique" style. This code has been one of the buggiest least robust parts of the entire project. So I am here asking if this is the best way? Is there a better Algorithm/Solution? Is the code just riddled with bugs?

In reply to Longest Matching URL by marceus

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.