map{$s1 =~ s/$_/[$_]/g} @a1; map{$s2 =~ s/$_/[$_]/g} @a2; map{$s3 =~ s/$_/[$_]/g} @a3; map{$s4 =~ s/$_/[$_]/g} @a4; print join("\n",$s1,$s2,$s3,$s4)."\n"; gives; CCC[ATCTG]TCCTT[ATTTG]CTG AC[CCATC]TGTCCTTGGCCAT [CCACC]A[GCACC]TGTC C[CCAAC]ACCTGCTGCCT Is this what you want? If not, Please give what is expected to happen for the last 2 strings. Hope this helps!