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

Update
for not wanting to include a module or steal the code from one, and no actual reason not to use a shell function other than "it wouldn't be pure Perl".
I'm still learning man and I want to learn perl not shell. I already tried to "steal" the code but you have no idea how many subroutines involves that module. I'm not so advanced to "steal" that code. I just want to find out a simpler way for:
string2 - string1 = difference
It's not necessary to become rude. I thought this is a place where perl gurus can share their experience with newbs like me. But nevermind.

Replies are listed 'Best First'.
Re^3: Comparing $_ --- before/after
by Marshall (Canon) on Oct 12, 2009 at 03:35 UTC
    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
      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!
Re^3: Comparing $_ --- before/after
by ssandv (Hermit) on Oct 11, 2009 at 06:17 UTC

    That doesn't even make sense. There's not a uniform meaning of string subtraction. If you'd answer my sample problems above, it would help. There's *not* a unique way. You have to decide and tell us if you want us to help you. You tell us by giving lots of good examples, not one bad one. This isn't a Perl problem, it's a failure to clearly specify intended results problem, which is language independent.

    Further, the gurus *did* share information with you, and you rejected all of it.