use strict; use warnings; my @strings = ( 'string that I need to gather the common base from: number 1 and some other junk in it', 'string that I need to gather the common base from: number 2 and some other junk in it', 'string that I need to gather the common base number 4 and some other junk in it', 'string that I need to gather the common base from: number 3 and some other junk in it', ); my $common = $strings[0]; for my $str (@strings[1 .. $#strings]) { ($common ^ $str) =~ m/^\0*/; $common = substr $str, 0, $+[0] if $+[0] < length $common; } print "'$common'"; #### 'string that I need to gather the common base '