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):
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?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); };
In reply to Longest Matching URL by marceus
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |