http://www.perlmonks.org?node_id=1006387

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks I have 2 strings in this way:
String1: "big business"
String2: "big movie companies"
What I want to generate is something like "big business and movie companies" which is the smart concatenation. If both strings has the same size and if the position of the common word is the same the problem is easier. There are issues when the position of the common token is not same. Any idea would be appreciated.
The example is very dummy but when the length and the positions are different, then the normal positioning cant be used.
for $i (0 .. $#p2) { #print "$p1[$i] vs $p2[$i]\n"; $mism_pos{$i} = 1 if ($p1[$i] +eq $p2[$i]); } #print sort keys %mism_pos,"\n"; if (keys %mism_pos) { for $i (0..$#p2) { if (defined $mism_pos{$i}) + { if (@new1 && @new2) { push @new,@new1,'a +nd',@new2,$p2[$i]; @new1=(); @new2=(); } else { push @new,$p2[$i]; } } else { push @new1,$p1[$i]; push @new2,$p2[$i]; } } }
This solution would work if the length and positions are the same. However, I get also this error: "Use of uninitialized value within @p1 in concatenation (.) or string" and I get this as answer: "big"