In article <[EMAIL PROTECTED]>, John W. Krahn wrote:

> Freddy söderlund wrote:
>> 
>> Let me re-phrase my question a bit:
>> 
>> I want to compare the two strings and I want to extract those chars that
>> are matching each other in the first and second string (in order from the
>> beginning), and put them in a new string (not array as I mistakenly said
>> earlier).
>> 
>> So, if I have
>> $string1 = "C:\Program files\directory1\directory2\directory3"
>> $string2 = "C:\Program files\directory1\dir2\dir3"
>> 
>> then I want the output to be $string3 = "C:\Program files\directory1\";
[...]

> 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...

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;

for (0 .. $length) {
    last if ($array1[$_] ne $array2[$_]);   # avoid trailing /
    push @diff, "\\" if @diff;
    push @diff, $array1[$_];

}

print "Common path", @diff, "\n";

-Kevin

-- 
Kevin Pfeiffer
International University Bremen
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to