in reply to Re^2: Find common prefix from a list of strings
in thread Find common prefix from a list of strings
sub maxprefix { my $p = quotemeta shift; for (@_) { chop $p until /^$p/ } return $p; }
This will still often fail on metacharacters because you'll get a "trailing \ in regexp" error.
sub maxprefix { my $p = shift(@_); for(@_) { chop $p until /^\Q$p/ } return $p; }
Update: The one you added has problems as well:
sub maxprefix { my $s = reverse shift; my $p = ''; for (@_) { $p .= quotemeta chop $s while /^$p/ } chop $p; return $p; }
It will find the maximum prefix between the first string and the string of the others that has the longest common prefix with the first string:
prints test not t.print maxprefix("testing","terse","tester","time"),$/;
- tye
In Section
Seekers of Perl Wisdom