use warnings; use strict; my ($s1, $s2, $o1, $o2) = ("AAABBBBBCCCCCDDDDD", "AEABBBBBCCECCDDDDD"); my @s1 = split(//, $s1); my @s2 = split(//, $s2); my $eq_state = 1; while (@s1 and @s2) { if (($s1[0] eq $s2[0]) != $eq_state) { $o1 .= (!$eq_state) ? "</b>" : "<b>"; $o2 .= (!$eq_state) ? "</b>" : "<b>"; } $eq_state = $s1[0] eq $s2[0]; $o1.=shift @s1; $o2.=shift @s2; } print "$o1\n$o2\n";
Exit
A<b>A</b>ABBBBBCC<b>C</b>CCDDDDD A<b>E</b>ABBBBBCC<b>E</b>CCDDDDD
The simplest one that only displays the second line:
use warnings; use strict; my ($s1, $s2, $was_eq) = ("AAABBBBBCCCCCDDDDD", "AEABBBBBCCECCDDDDD", 1); my @s1 = split(//, $s1); my @s2 = split(//, $s2); for my $idx (0 .. @s2 -1) { my $is_eq = $s1[$idx] eq $s2[$idx]; print $is_eq ? "</b>" : "<b>" if ( $was_eq != $is_eq); $was_eq = $is_eq; print $s2[$idx]; }
Outout
</b>A<b>E</b>ABBBBBCC<b>E</b>CCDDDDD
perreal
source share