in reply to Find the common beginning substring in an array of strings
Assuming a character set with a single-character-to-byte mapping:
>perl -wMstrict -le "use List::Util qw(reduce); ;; my @ra = qw(ABCDxyz ABCfoo ABCDEbar); ;; use vars qw($a $b); my $min_start_seq = reduce { length($a) < length($b) ? $a : $b } map m{ \A \x00* }xmsg, map $ra[0] ^ $_, @ra ; my $common_starting_substring = $ra[0] & ~$min_start_seq; print qq{'$common_starting_substring'}; " 'ABC'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the common beginning substring in an array of strings
by technojosh (Priest) on Jun 06, 2012 at 18:56 UTC | |
by aaron_baugher (Curate) on Jun 06, 2012 at 19:27 UTC | |
by Neighbour (Friar) on Jun 07, 2012 at 07:59 UTC |