in reply to Re: Find the common beginning substring in an array of strings (Updated)
in thread Find the common beginning substring in an array of strings
Not sure if I miss something, but it seems there is a problem.
With this array for example:
output: History--amy @array_of_test_names = ( "History--abcdef", "History--aaa", "History--123", "History--1X", );
updated: bad try
A try:
my $mask = ''; $mask ^= $_ ^ $array_of_test_names[0] for @array_of_test_names; $mask =~ m[^(\0+)] and $len = length( $1 ); print substr $array_of_test_names[ 0 ], 0, $len; print "\n";
update2: btw, a not pretty solution
my $res = ''; for my $i ( 0 .. (length($array_of_test_names[0]) -1)) { my $letter = substr ($array_of_test_names[0],$i,1); last if ( scalar grep { $letter ne substr($array_of_test_names[$_] +,$i,1)} (0 .. $#array_of_test_names)); $res .= $letter; }; print $res,"\n";
|
|---|