It would be helpful if you could give say 5-6 examples of what you want, meaning input strings and desired output.

The general problem of how to compare text and determine "how close" one string is to another is very complex.

I'm not quite sure, but I think you just need some code that points out where the 2 strings diverge (first char that doesn't match). The simple code below is a step in that direction. I'm sure you will need to modify this to your needs, but I hope this gets you started on the "path".

#!usr/bin/perl -w use strict; my $string1 = "abcdf"; my $string2 = "abcde"; #for this simple comparision, I assume that using #the longest string as the iterator value #which will be "string1", is "better". if ( length($string2) > length ($string1) ) { ($string1,$string2) = ($string2,$string1); #in Perl an intermediate "temp" is not necessary } my @string2 = split (//,$string2); #array of characters my $result=""; foreach (split (//,$string1)) #loop var is $_ over chars in #string1 { my $char = shift (@string2); if (defined ($char) && $char eq $_ ) { $result .= "1"; } else { $result .= $_; } } print "CharbyCharResult =$result\n"; print "difference is:",grep{!/1/}split(//,$result),"\n"; __END__ EXAMPLE RUNS: my $string1 = "abcdf"; my $string2 = "abcde"; CharbyCharResult =1111f difference is:f my $string1 = "abcdfccc"; my $string2 = "abcde"; CharbyCharResult =1111fccc difference is:fccc my $string1 = "abcdf"; my $string2 = "abcdeeeee"; CharbyCharResult =1111eeeee difference is:eeeee my $string1 = "abcdfxy"; my $string2 = "abcdexzy"; CharbyCharResult =1111e1zy difference is:ezy

In reply to Re^3: Comparing $_ --- before/after by Marshall
in thread Comparing $_ --- before/after by mrc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.