Professional Web Applications Themes
  1. #1

    Default Compare two array's

    Hi!,

    I know that this is a no brainer, but I cannot not get it! I want to
    compare two arrays and delete the same values in one array.

    foreach $j ( @a1 ) {
    foreach $p ( @a2 ) {
    $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
    }
    }

    What is wrong and is there a better, PERL way to do this?

    Thanks,

    Jerry

    Jerry Preston Guest

  2. #2

    Default Re: Compare two array's

    Maybe the missing opening bracket on the third line?
    >$a2[$p] = undef if $a1[$j] == $a2[$p];
    Being a beginner myself, I cannot recommend a better way.

    - Jan

    Jerry Preston wrote:
    >Hi!,
    >
    >I know that this is a no brainer, but I cannot not get it! I want to
    >compare two arrays and delete the same values in one array.
    >
    >foreach $j ( @a1 ) {
    > foreach $p ( @a2 ) {
    > $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
    > }
    >}
    >
    >What is wrong and is there a better, PERL way to do this?
    --
    There are 10 kinds of people: those who understand binary, and those who don't
    Jan Eden Guest

  3. #3

    Default Re: Compare two array's

    Jerry Preston wrote:
    >
    > Hi!,
    Hello,
    > I know that this is a no brainer, but I cannot not get it! I want to
    > compare two arrays and delete the same values in one array.
    >
    > foreach $j ( @a1 ) {
    > foreach $p ( @a2 ) {
    > $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
    > }
    > }
    >
    > What is wrong and is there a better, PERL way to do this?
    No, but there is a Perl way to do it: :-)

    @a2 = do {
    my %seen;
    @seen{ @a1 } = ( 1 ) x @a1;
    grep !$seen{ $_ }, @a2;
    };



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  4. #4

    Default Re: Compare two array's

    Jerry Preston wrote:
    >
    > I know that this is a no brainer, but I cannot not get it! I want to
    > compare two arrays and delete the same values in one array.
    >
    > foreach $j ( @a1 ) {
    > foreach $p ( @a2 ) {
    > $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
    > }
    > }
    >
    > What is wrong and is there a better, PERL way to do this?
    The first thing that's wrong is you're not starting with

    use strict;
    use warnings;

    Your code doesn't work because you're iterating over the
    values in the arrays instead of the indices. This will
    work as you intended

    foreach my $j (0 .. $#a1) {
    foreach my $p (0 .. $#a2) {
    $a2[$p] = undef if $a1[$j] == $a2[$p];
    }
    }

    but you'll get warnings when undefined elements of
    @a2 are being compared.

    The Perl way to do it is to build a hash of the elements
    of @a1 like this

    my %a1 = map(($_, 1), @a1);
    foreach (@a2) { $_ = undef if $a1{$_} };

    HTH,

    Rob


    Rob Dixon Guest

Similar Threads

  1. Compare 2 PDF
    By Claus_Allgeier@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 2
    Last Post: December 10th, 02:08 PM
  2. compare
    By Piet in forum MySQL
    Replies: 3
    Last Post: April 9th, 01:57 AM
  3. array data matches but array created in loop doesn't work
    By Reed Law in forum PHP Development
    Replies: 1
    Last Post: August 13th, 04:25 AM
  4. #24897 [Com]: array_multisort() will reindex the array but not if array length is 1
    By franklin_se at hotmail dot com in forum PHP Development
    Replies: 0
    Last Post: August 2nd, 03:29 PM
  5. Replies: 0
    Last Post: August 1st, 12:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139