#!/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 Remove(-) Icon of Relative. - Clinic', 'History - Family History - Suite with Patient - Clinic - Save after Entering values in all fields - Clinic', 'History - Family History - Suite with Patient - Clinic - Select Add(+) Icon of Relative. - Clinic', 'History - Family History - Suite with Patient - Clinic - Multiple selection of relatives - Clinic', 'History - Family History - Suite with Patient - Clinic - Interaction Checking message not provided if procedure code is selected - Clinic', 'History - Family History - Suite with Patient - Clinic - Interaction Checking message provided if no procedure code is selected - Clinic', ); my $match = findstr(@array_of_test_names); say $match;