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\";
Backslashes are interpolated in double quoted strings so you need to either escape them or use single quoted strings or use slashes instead of backslashes. use warnings; use strict; 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/[^\\]*$//; print <<TEXT; $string1 $string2 $string3 TEXT John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]