perl compare two strings and highlight mismatch characters -


string1 = "aaabbbbbcccccddddd" string2 = "aeabbbbbcceccddddd" 

output. mismatch (in case e) replaced html tags around e color it.

a**e**abbbbbcc**e**ccddddd 

what tried far: xor, diff , substr. first need find indices replace indices pattern.

use strict; use warnings; $string1 = 'aaabbbbbcccccddddd'; $string2 = 'aeabbbbbcceccddddd'; $result = ''; for(0 .. length($string1)) {     $char = substr($string2, $_, 1);     if($char ne substr($string1, $_, 1)) {         $result .= "**$char**";     } else {         $result .= $char;     } } print $result; 

prints a**e**abbbbbcc**e**ccddddd

tested somewhat. may contain errors.


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -