Ask a Question related to PERL Beginners, Design and Development.

  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. Similar Questions and Discussions

    1. Compare 2 PDF
      Hi, is there any way to automate the comparison of two PDFs from outside Acrobat? e.g. I mark 2 PDFs, right-click offers me COMPARE and after...
    2. compare
      How can I stop a function in PHP? When comparing data I want to stop the function on a hit. Function compare($data) { For...
    3. compare excel
      I have an application needs to merge two excel files and remove duplicates, is there a custom tag that I can use or any suggestions how I should go...
    4. Compare 2 documents
      I use Compare two documents feature alot and it is very time consuming. I would like to find a way to automate this. The document path and file name...
    5. Compare strings
      Hi, I'm trying to see if a certain string matches a predefined string so I've tried: -------------- If txtCondition3 = "NO THEFT COVER unless...
  3. #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

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

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

Posting Permissions

  • You may not post new threads
  • You may 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