in reply to Selecting the difference between two strings

Not And or Or, but Xor (^):

use strict; use warnings; my $str1 = '//depot/Efp/Celox/CELOX-3.5.0/CeloxStart.sln'; my $str2 = '//depot/Efp/Celox/MAIN/CeloxStart.sln'; my $str1r = $str1; my $str2r = $str2; # Find and strip common prefix my $match = ($str1 ^ $str2) =~ /^(\x00*)/; substr $str1r, 0, $+[1], '' if $match; substr $str2r, 0, $+[1], '' if $match; # Find and strip common suffix $match = (reverse ($str1) ^ reverse ($str2)) =~ /^(\x00*)/; substr $str1r, -$+[1], length ($str1r), '' if $match; substr $str2r, -$+[1], length ($str2r), '' if $match; print "$str1r\n$str2r\n";

Prints:

CELOX-3.5.0 MAIN

Update: process both strings


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Selecting the difference between two strings
by qazwart (Scribe) on Sep 27, 2006 at 04:32 UTC
    There we go!

    I figured out that it was XOR on my way home, but I wasn't quite sure how to apply it. Plus, I forgot about the reverse function which is what I need in order to strip the end of the string. Instead, I was going to pad left the strings to make them the same length.

    Thank you for your answer. I'll try it out when I get to work tomorrow.