FAQ
Hi!

I have stuck my thoughts on this one, maybee someone can help me out?

I have two arrays

@array1 = "C:\Program files\directory1\directory2\directory3";
@array2 = "C:\Program files\directory1\dir2\dir3";

What I want to do is, compare the arrays, one char at a time, from the beginning and stop when I find two chars that are different, putting the matching chars in a new scalar.

Any suggestions?

Greatfull for any help!

/Freddy

Search Discussions

  • Rob Anderson at Sep 4, 2003 at 1:28 pm

    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in
    message news:010301c372e1$6a5158f0$6400a8c0@winter...
    Hi! Hi
    I have stuck my thoughts on this one, maybee someone can help me out?

    I have two arrays

    @array1 = "C:\Program files\directory1\directory2\directory3";
    @array2 = "C:\Program files\directory1\dir2\dir3";
    It looks like you're trying to build an array, each element of which is a
    single character from your string. What you've actually got here is two
    arrays both each holding a single string.
    my @string_array = split('', "onetwothree");
    this would build an array with an element for each character. However you
    can quickly compare this strings in the way you want without resorting to
    arrays. I've posted an answer to a similar question on this list here...

    http://groups.google.com/groups?q=Comparing+two+strings+for+first+difference
    &ie=UTF-8&oe=UTF-8&hl=en

    Good luck
    What I want to do is, compare the arrays, one char at a time, from the
    beginning and stop when I find two >chars that are different, putting the
    matching chars in a new scalar.
    Any suggestions?

    Greatfull for any help!

    /Freddy
  • Freddy Söderlund at Sep 4, 2003 at 2:33 pm
    ----- Original Message -----
    From: "Rob Anderson" <rjanderson@uk2.net>
    To: <beginners@perl.org>
    Sent: Thursday, September 04, 2003 3:30 PM
    Subject: Re: Extracting equal entities from two different sized arrays?

    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in
    message news:010301c372e1$6a5158f0$6400a8c0@winter...
    Hi!
    Hi
    Hi again!

    Thanx for your time!

    Got time for an elaboration?
    I have stuck my thoughts on this one, maybee someone can help me out?

    I have two arrays

    @array1 = "C:\Program files\directory1\directory2\directory3";
    @array2 = "C:\Program files\directory1\dir2\dir3";
    It looks like you're trying to build an array, each element of which is a
    single character from your string. What you've actually got here is two
    arrays both each holding a single string.
    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\";
    my @string_array = split('', "onetwothree");
    this would build an array with an element for each character. However you
    can quickly compare this strings in the way you want without resorting to
    arrays. I've posted an answer to a similar question on this list here...

    http://groups.google.com/groups?q=Comparing+two+strings+for+first+difference
    &ie=UTF-8&oe=UTF-8&hl=en
    I gave you the wrong flightplan so your rockets didn't take me all the way
    to the moon. ;-)
    Your example works good but it can't give me the resulting 3:rd string. It
    only gives me the number of how many characters that matches.
    Good luck
    What I want to do is, compare the arrays, one char at a time, from the
    beginning and stop when I find two >chars that are different, putting the
    matching chars in a new scalar.
    Any suggestions?

    Greatfull for any help!

    /Freddy




    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • Rob Anderson at Sep 4, 2003 at 2:48 pm
    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in message
    news:011c01c372f0$cabd31a0$6400a8c0@winter...
    ----- Original Message -----
    From: "Rob Anderson" <rjanderson@uk2.net>
    To: <beginners@perl.org>
    Sent: Thursday, September 04, 2003 3:30 PM
    Subject: Re: Extracting equal entities from two different sized arrays?

    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in
    message news:010301c372e1$6a5158f0$6400a8c0@winter...
    Hi!
    Hi
    Hi again! [snip]
    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). [snip]
    I gave you the wrong flightplan so your rockets didn't take me all the way
    to the moon. ;-)
    Your example works good but it can't give me the resulting 3:rd string. It
    only gives me the number of how many characters that matches.

    but you're so close now!. You know how many characters match at the front of
    the string, so just substring them off. He's a working example, which is
    spoon feeding a bit, but I guess you don't know the substr function
    (perldoc -f substr)

    #!/perl -w
    use strict;

    my $string1 = "The quick bruwn fox.";
    my $string2 = "The quick brown fox.";

    my $string_xor = ("$string1" ^ "$string2");
    $string_xor =~ /^(\0*)/;
    my $matching_char_count = length($1);
    my $string3 = substr($string1, 0, $matching_char_count);
    print $string3 . "<==\n";


    HTH

    Rob Anderson
  • Freddy Söderlund at Sep 4, 2003 at 2:56 pm
    ----- Original Message -----
    From: "Rob Anderson" <rjanderson@uk2.net>
    To: <beginners@perl.org>
    Sent: Thursday, September 04, 2003 4:50 PM
    Subject: Re: Extracting equal entities from two different sized arrays?

    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in message
    news:011c01c372f0$cabd31a0$6400a8c0@winter...
    ----- Original Message -----
    From: "Rob Anderson" <rjanderson@uk2.net>
    To: <beginners@perl.org>
    Sent: Thursday, September 04, 2003 3:30 PM
    Subject: Re: Extracting equal entities from two different sized arrays?

    ""Freddy söderlund"" <freddy_soderlund@chello.se> wrote in
    message news:010301c372e1$6a5158f0$6400a8c0@winter...
    Hi!
    Hi
    Hi again! [snip]
    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). [snip]
    I gave you the wrong flightplan so your rockets didn't take me all the
    way
    to the moon. ;-)
    Your example works good but it can't give me the resulting 3:rd string.
    It
    only gives me the number of how many characters that matches.

    but you're so close now!. You know how many characters match at the front of
    the string, so just substring them off. He's a working example, which is
    spoon feeding a bit, but I guess you don't know the substr function
    (perldoc -f substr)
    /me is going to the "shame-on-me-corner" now. ;-)

    I think I need to look deeper into my coffe-cup or maybe I need to sleep.
    Once again, you're correct. I tried the code and it works like a charm.
    Surely, I know the substr function but it never came to me.

    I suffer from the "to-easy-to-solve" syndrome.
    If it's too easy to solve, I can't solve it since my thoughts are stuck on
    the complicated things.

    Thanx for your help!

    It saved my day and a lot of headache!
    #!/perl -w
    use strict;

    my $string1 = "The quick bruwn fox.";
    my $string2 = "The quick brown fox.";

    my $string_xor = ("$string1" ^ "$string2");
    $string_xor =~ /^(\0*)/;
    my $matching_char_count = length($1);
    my $string3 = substr($string1, 0, $matching_char_count);
    print $string3 . "<==\n";


    HTH

    Rob Anderson



    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
  • James Edward Gray II at Sep 4, 2003 at 2:50 pm

    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
  • John W. Krahn at Sep 4, 2003 at 6:04 pm

    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
  • Kevin Pfeiffer at Sep 7, 2003 at 9:20 pm

    In article <3F577EA5.66560221@acm.org>, 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?
  • John W. Krahn at Sep 7, 2003 at 10:41 pm

    Kevin Pfeiffer wrote:

    In article <3F577EA5.66560221@acm.org>, 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
  • Kevin Pfeiffer at Sep 8, 2003 at 8:34 am

    In article <3F5BB42B.8B075CBC@acm.org>, John W. Krahn wrote:

    Kevin Pfeiffer wrote:
    In article <3F577EA5.66560221@acm.org>, 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\'.
    Thanks!
    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;
    I had this in and removed it at the last minute, deciding that it wasn't
    necessary (but forgot to change the next line in my already copied-in
    version)...

    for (0 .. $length) {
    last if ($array1[$_] ne $array2[$_]); # avoid trailing /
    push @diff, "\\" if @diff;
    push @diff, $array1[$_];
    [...]
    --
    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?

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 4, '03 at 12:42p
activeSep 8, '03 at 8:34a
posts10
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase