With a minor correction: [or three or four--that was definitely posted in haste] so that it runs: #!/usr/bin/perl -w
use strict; use warnings; my $word1 = "Hello"; my $word2 = "Othello's World"; my $result = difference($word1, $word2); print "$result\n"; sub difference { my ($word1, $word2) = @_; if ($word1 =~ /.*$word2.*/i) { $word1 =~ s/(.*)$word2(.*)/$1$2/i; return $word1; } elsif ($word2 =~ /.*$word1.*/i) { $word2 =~ s/(.*)$word1(.*)/$1$2/i; return $word2; } } E:\d_drive\perlStuff\guests>StringDifference.pl Ot's World "R. Joseph Newton" wrote: > Hi John, > > Hmmm, I don't know about this handing ready-made code out, but--what the hell, the >harms been done, so I';ll join the party: > > #!/usr/bin/perl -w > > use strict; > use warnings; > > my $word1 = "Hello"; > my $word2 = "HelloWorld"; > > sub difference { > my ($word1, $word2) = @_; > if ($word1 =~ /.*$word2.*) { > $result = ($word1 =~ s/(.*)$word2(.*)/$1$2/) > } elsif ($word2 =~ /.*$word1.*) { > $result = ($word2 =~ s/(.*)$word1(.*)/$1$2/) > } else { > $result = ""; > } > return $result; > } > > For the sake of comprehension. > > Joseph > > "John W. Krahn" wrote: > > > Alex Demmler wrote: > > > > > > Hi folks! > > > > Hello, > > > > > I have a problem with pattern matching. > > > I want do find the difference between two words. I have tried, but donot get > > > it work. Any tips? > > > > > > Example: > > > > > > 1. Word = helloRed > > > 2. Word = hello > > > ------------------- > > > Result = Red > > > > my $word1 = 'helloRed'; > > my $word2 = 'hello'; > > > > # make sure that $word1 is longer than word2 > > ( $word1, $word2 ) = ( $word2, $word1 ) if length( $word1 ) < length( $word2 ); > > > > ( my $result = $word1 ) =~ s/\Q$word2//; > > > > print "$result\n"; > > > > John > > -- > > use Perl; > > program > > fulfillment > > > > -- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]