in reply to Re^2: Comparing $_ --- before/after
in thread Comparing $_ --- before/after

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

Replies are listed 'Best First'.
Re^4: Comparing $_ --- before/after
by mrc (Sexton) on Oct 12, 2009 at 06:28 UTC
    Pure and simple! Thank you Marshall and thank you guys for your advices.
      Glad to help out. Perl eq MAGIC. It is a great language, keep going!