in reply to Common Substrings
This code;
behaves the same as the last one you posted. It's more efficient as well as simpler (according to my benchmarks).sub common_path { my ($p, $q) = ("$_[0]/", "$_[1]/"); (my $pos = ($p ^ $q)) =~ /[^\0]/g; my $i = 1 + rindex($p, "/", pos($pos) - 2); return ( substr($p, 0, $i), substr($p, $i, -1), substr($q, $i, -1) ); }
I don't think there's any good reason to use bytes for this. Were you doing it for efficiency reasons?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Common Substrings
by Anonymous Monk on Nov 16, 2005 at 04:24 UTC |
In Section
Seekers of Perl Wisdom