=item my $prefix = shortest_prefix @strings; Return the shortest prefix common to all @strings. Returns empty string if there is no prefix; returns C if there are no strings or if any of the values are themselves C =cut # algorithm by Ilya Z # implementation by Ken Fox # both ganked from c.l.p.misc: # http://groups.google.fm/groups?selm=199908100340.UAA13944%40long-lake.nihongo.org sub common_prefix ( @ ) { defined( my $prefix = shift ) or return undef; my $len = length $prefix or return ''; foreach my $w ( @_ ) { defined $w or return undef; while ( substr( $w, 0, $len ) ne $prefix ) { --$len; chop $prefix; } $len or return ''; } return $prefix; }