in reply to Comparing $_ --- before/after

So, this is going to be the part where people are going to start getting snarky. You've waved your hands and said "I want the differences" without explaining what you mean, you gave exactly one toy example which is hardly sufficient to specify anything, and you've given weak reasons 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". (Don't you think the code in a module is likely to be reasonably compact and solid? Why do you expect to get something better here?)

If you want a decent answer, you'll have to explain yourself thoroughly. (And tell us what the full problem you're attempting to solve is, this sounds like an XY problem. Or homework.) Your criteria sound increasingly absurd without a better explanation. Is your next complaint going to be that you want a one-liner?

Define and explain the expected result for the following, and we might get *somewhere*:

"Banana" / "banana" "banana" / "Banana" "Bananananafofana" / "Bafana" "Bafana" / "Bananananafofana" "Banana" / "anaBanana" "anaBanana" / "Banana" "Banana" / "Bazaza" "Banana" / "Bannana" "Bannana" / "Banana"

Or else, do what I said up above, and do the printing immediately after the substitution when you know what you've swapped out. (Hint: the different letters are probably whatever's in the right hand side of the s/// if the substitution works, at least for your example where it's a simple string.)

Replies are listed 'Best First'.
Re^2: Comparing $_ --- before/after
by mrc (Sexton) on Oct 11, 2009 at 06:12 UTC
    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.
      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.

      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.