By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Match between the arrays

P: 72
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

Expand|Select|Wrap|Line Numbers
  1. @arrayA = qw (A,3 B,4 D,5 E,6 ); 
  2. @arrayB = qw (A,3 B,5 C,5);  
  3.  
  4. my @NotInB = do {
  5. my %inA = map { $_ ,1} (@arrayB,(split',')[0]);
  6. grep (!$inA{$_}, (@arrayA,(split',')[0]));
  7. };
  8.  
  9. 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
May 7 '08 #1
Share this Question
Share on Google+
9 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
This should get you started on the path to comparing your arrays. Let us know if you get stuck.

Regards,

Jeff
May 7 '08 #2

nithinpes
Expert 100+
P: 410
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:
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. @arrayA = qw (A,3 B,4 D,5 E,6 ); 
  4. @arrayB = qw (A,3 B,5 C,5);  
  5.  
  6. my %inA = map { $_ ,1}  map { ((split /,/,$_)[0])} (@arrayB);
  7.  
  8. foreach(@arrayA) {
  9. my $temp= (split /,/,$_)[0] ;
  10. push @NotInB,$_ if(!(exists $inA{$temp}));  
  11. }
  12.  
  13. print Dumper @NotInB;
  14.  
May 7 '08 #3

P: 72
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.
May 7 '08 #4

nithinpes
Expert 100+
P: 410
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(alphabets 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.
May 8 '08 #5

nithinpes
Expert 100+
P: 410
Here it is:
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. @arrayA = qw (A,3 B,4 D,5 E,6 ); 
  3. @arrayB = qw (A,3 B,5 C,5);  
  4. #splitting elements initial array across ',' and creating a new array
  5. push @splitA,split(/,/,$_) foreach (@arrayA);
  6.  
  7. %hashA = @splitA; 
  8. foreach(@arrayB) {
  9. # split the element into key-value pair
  10. my ($key,$val)= (split /,/,$_);
  11.  
  12. if(!(exists $hashA{$key})) {
  13. push @result,$_ ;
  14. } else {
  15. my $diff=abs($val-$hashA{$key}) ; # absolute diff. between respective numbers
  16. my $str = join(',',($key,$diff));
  17. push @result,$str ;
  18. }
  19. }
  20. print Dumper @result;
  21.  
  22.  
May 8 '08 #6

P: 72
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.
May 9 '08 #7

nithinpes
Expert 100+
P: 410
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(until you match an alphabet) to it's value(array).

- Nithin
May 9 '08 #8

nithinpes
Expert 100+
P: 410
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.
Expand|Select|Wrap|Line Numbers
  1. @arrayA = qw(A,3,4, B,8,7, C,9,3);
  2. push @splitA,split(/,/,$_,2) foreach(@arrayA );
  3. ## convert it to a hash (one-dimensional)
  4. %hashA=@splitA;
  5.  
  6. ## create hash of arrays
  7. foreach(keys %hashA) {
  8. # split the comma-separated numbers in value into an anonymous array
  9.  $newhashA{$_} = [split /,/,$hashA{$_}];  
  10. }
  11.  
  12. print Dumper %newhashA;
  13.  
May 9 '08 #9

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

:)
May 9 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.