Kevin Pfeiffer wrote: > > In article <[EMAIL PROTECTED]>, John W. Krahn wrote: > > > my $string1 = 'C:\Program files\directory1\directory2\directory3'; > > my $string2 = 'C:\Program files\directory1\dir2\dir3'; > > > > ( my $nulls = $string1 ^ $string2 ) =~ s/^(\0+).*/$1/s; > > > > ( my $string3 = substr $string1, 0, length $nulls ) =~ s/[^\\]*$//; > > Wish I understood how that works...
^ is the exclusive or (xor) operator. If you xor two different numbers you will get a non-zero number and if you xor two identical numbers you will get zero. Xor on strings xors each character in the two strings so that characters that are the same in both strings will become "\0" (ASCII NUL). The substitution removes everything after the leading NULs so you are left with a string of NULs the length of which is the number of characters at the beginning of the first two strings that are identical. In the example above, that puts a string of 31 "\0" characters in the variable $nulls. The 31 character string at the beginning of $string1 (or $string2, it doesn't matter which) is assigned to $string3 ('C:\Program files\directory1\dir') and any non-'\' character is removed from the end so that $string3 finally contains 'C:\Program files\directory1\'. > Here's my novice version (but I'm guessing the better/portable way is to use > File::Spec?)... > > my @array1 = split /\\/, $string1; > my @array2 = split /\\/, $string2; > my @diff; my $length = @array1 < @array2 ? @array1 : @array2; > for (0 .. $length) { > last if ($array1[$_] ne $array2[$_]); # avoid trailing / > push @diff, "\\" if @diff; > push @diff, $array1[$_]; > > } > > print "Common path", @diff, "\n"; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]