On Thursday, September 4, 2003, at 09:28 AM, Freddy Söderlund wrote:

Another example on just how *stuck* I was! You're right ofcourse and I were
wrong when typing my example.
It's really two strings: $string1 and $string2.


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\";

How about something like this?


my $string3 = '';
for (my $i = 0; $i < length($sting1) || $i < length($string2); $i++) {
        if (substr($string1, $i, 1) eq substr($string2, $i, 1)) {
                $string3 = substr $string1, 0, $i + 1;
        }
        else { last; }
}

This just compares a character at a time, to see if they are the same. If they are, we store that much. If not, we're all done.

Hope that helps.

James

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



Reply via email to