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"),$/;
In reply to Re^3: Find common prefix from a list of strings (trailing \)
by tye
in thread Find common prefix from a list of strings
by Albannach
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |