In that case, though I'm certain the masking method others have posted is the best solution, here's a recursive/substr method, just for fun:
#!/usr/bin/env perl use Modern::Perl; sub findstr { if( @_ == 1 ){ return shift; } my $x = shift; my $y = shift; my $l = length $x > length $y ? length $y : length $x; for( my $i=$l; $i>0; $i--){ if( substr($x, 0, $i) eq substr($y, 0, $i)){ return findstr(substr($x,0,$i), @_); } } } my @array_of_test_names = ( 'History - Family History - Suite with Patient - Clinic - Select R +emove(-) Icon of Relative. - Clinic', 'History - Family History - Suite with Patient - Clinic - Save aft +er Entering values in all fields - Clinic', 'History - Family History - Suite with Patient - Clinic - Select A +dd(+) Icon of Relative. - Clinic', 'History - Family History - Suite with Patient - Clinic - Multiple + selection of relatives - Clinic', 'History - Family History - Suite with Patient - Clinic - Interact +ion Checking message not provided if procedure code is selected - Cli +nic', 'History - Family History - Suite with Patient - Clinic - Interact +ion Checking message provided if no procedure code is selected - Clin +ic', ); my $match = findstr(@array_of_test_names); say $match;
Aaron B.
Available for small or large Perl jobs; see my home node.
In reply to Re^3: Find the common beginning substring in an array of strings
by aaron_baugher
in thread Find the common beginning substring in an array of strings
by technojosh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |