Match between the arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    Match between the arrays

    there are two arrays in arrayA and arrayB, I wish to match the list between them regardless of the number(1 2 3 or...) to list down which is not in arrayB compare to arrayA

    Code:
    @arrayA = qw (A,3 B,4 D,5 E,6 ); 
    @arrayB = qw (A,3 B,5 C,5);  
    
    my @NotInB = do {
    my %inA = map { $_ ,1} (@arrayB,(split',')[0]);
    grep (!$inA{$_}, (@arrayA,(split',')[0]));
    };
    
    print Dumper @NotInB;
    suppose i wish to get
    $VAR1 = 'D,5';
    $VAR2 = 'E,6';

    but i get the result as...
    $VAR1 = 'B,4';
    $VAR2 = 'D,5';
    $VAR3 = 'E,6';

    please guide... thanks
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    This should get you started on the path to comparing your arrays. Let us know if you get stuck.

    Regards,

    Jeff

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      I am not very clear on your question. Is that you need to consider only the alphabets in the alphabet-number pairs while comparing those two arrays?
      In that case, the following code will do the job:
      Code:
      use Data::Dumper;
      
      @arrayA = qw (A,3 B,4 D,5 E,6 ); 
      @arrayB = qw (A,3 B,5 C,5);  
      
      my %inA = map { $_ ,1}  map { ((split /,/,$_)[0])} (@arrayB);
      
      foreach(@arrayA) {
      my $temp= (split /,/,$_)[0] ;
      push @NotInB,$_ if(!(exists $inA{$temp}));  
      }
      
      print Dumper @NotInB;
      Last edited by nithinpes; May 7 '08, 11:49 AM. Reason: edited text

      Comment

      • MyMarlboro
        New Member
        • Mar 2008
        • 71

        #4
        Yup, that's what i want. Thank you both of you. I appreaciate it.

        I have last question, how if i wish to deduct the number when i compare arrayA and arrayB if it's exist in arrayB.
        result as below:
        $VAR1 = 'A,0';
        $VAR2 = 'B,1';
        $VAR3 = 'C,5';

        Please give me some hints on how to do the it. Thanks.

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by MyMarlboro
          Yup, that's what i want. Thank you both of you. I appreaciate it.

          I have last question, how if i wish to deduct the number when i compare arrayA and arrayB if it's exist in arrayB.
          result as below:
          $VAR1 = 'A,0';
          $VAR2 = 'B,1';
          $VAR3 = 'C,5';

          Please give me some hints on how to do the it. Thanks.
          For this, you can split both arrays further on commas and assign the resulting arrays to hashes(alphabet s as keys and succeeding numbers as respective values).
          Further you can parse through @arrayB and take out the alphabets, as in previous code. Try to check if the key exists in %hashA, if so substract their respective values and push the key and resulting value to result array.

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            Here it is:
            Code:
            use Data::Dumper;
            @arrayA = qw (A,3 B,4 D,5 E,6 ); 
            @arrayB = qw (A,3 B,5 C,5);  
            #splitting elements initial array across ',' and creating a new array
            push @splitA,split(/,/,$_) foreach (@arrayA);
            
            %hashA = @splitA; 
            foreach(@arrayB) {
            # split the element into key-value pair
            my ($key,$val)= (split /,/,$_);
            
            if(!(exists $hashA{$key})) {
            push @result,$_ ;
            } else {
            my $diff=abs($val-$hashA{$key}) ; # absolute diff. between respective numbers
            my $str = join(',',($key,$diff));
            push @result,$str ;
            }
            }
            print Dumper @result;

            Comment

            • MyMarlboro
              New Member
              • Mar 2008
              • 71

              #7
              Hi nithinpes,
              Thanks.
              I try to play around with your codes.. where I added more number (A,3 --> A,3,4)
              as below:
              @arrayA = qw(A,3,4, B,8,7, C,9,3);
              @arrayB = qw(A,5,2 B,5,3 T,9,2);

              By that, your above code no longer can be reused. I think i should create the hashes of arrays right? e.g %hash = ('A' => ['3', '4'], 'B' => ['8','7'], 'C' => ['9','3']);
              Is it the way to do it? If so, how to created the hashes of arrays?
              Thanks.

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                Originally posted by MyMarlboro
                Hi nithinpes,
                Thanks.
                I try to play around with your codes.. where I added more number (A,3 --> A,3,4)
                as below:
                @arrayA = qw(A,3,4, B,8,7, C,9,3);
                @arrayB = qw(A,5,2 B,5,3 T,9,2);

                By that, your above code no longer can be reused. I think i should create the hashes of arrays right? e.g %hash = ('A' => ['3', '4'], 'B' => ['8','7'], 'C' => ['9','3']);
                Is it the way to do it? If so, how to created the hashes of arrays?
                Thanks.
                Well, the code that I posted was particular to the type of sample array that you posted(an alphabet followed by a number). If you want to extend this to alphabet followed by any number of numeric characters, creating hash of arrays is the good option.

                You can parse each element of the array and do pattern match. If you get an alphabetic character, make it the key and push the following numeric characters(unti l you match an alphabet) to it's value(array).

                - Nithin

                Comment

                • nithinpes
                  Recognized Expert Contributor
                  • Dec 2007
                  • 410

                  #9
                  I feel the following would be a better approach. The third argument of split() function will define the number of elements to be split into. In this case it is 2, the first element would be the alphabet and the next element will be comma-separated numbers.
                  Code:
                  @arrayA = qw(A,3,4, B,8,7, C,9,3);
                  push @splitA,split(/,/,$_,2) foreach(@arrayA );
                  ## convert it to a hash (one-dimensional)
                  %hashA=@splitA;
                  
                  ## create hash of arrays
                  foreach(keys %hashA) {
                  # split the comma-separated numbers in value into an anonymous array
                   $newhashA{$_} = [split /,/,$hashA{$_}];  
                  }
                  
                  print Dumper %newhashA;

                  Comment

                  • MyMarlboro
                    New Member
                    • Mar 2008
                    • 71

                    #10
                    Hi nithinpes,
                    THANKS you so much for your continuous guidance and help.
                    I learnt a lot from you.
                    Thank you & God Bless.

                    :)

                    Comment

                    Working...