Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Longest Matching URL

by AidanLee (Chaplain)
on Mar 20, 2002 at 17:58 UTC ( [id://153050]=note: print w/replies, xml ) Need Help??


in reply to Longest Matching URL

untested:

my @candidates = qw( http://www.someplace.com/foo http://www.someplace.com/foo/bar http://www.someplace.else.com/foo ); my $target = 'http://www.someplace.com/foo/fighter/index.html'; my %leader = ( uri => '', length => 0 ); foreach my $candidate (@candidates) { if( $target =~ /^$candidate/ ) { if( length($candidate) > $leader{length} ) { @leader{'uri','length'} = ($candidate,length($candidate)); } } } if( $leader{uri} ) { print $leader{uri},',',substr($target,$leader{length}); } else { print "no matches" }

Replies are listed 'Best First'.
Re: Re: Longest Matching URL
by marceus (Initiate) on Mar 20, 2002 at 19:58 UTC
    That works beautifully ... thanks.

    I remember I initally kept clearn of length() because I was afraid it would cut off at odd places in the URL. I suppose like most new (or new again) coders I forgot my KISS principle.

    Thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://153050]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2025-07-11 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.