Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Match between the arrays

Question posted by: MyMarlboro (Member) on May 7th, 2008 09:27 AM
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
numberwhun's Avatar
numberwhun
Forum Leader
2,032 Posts
May 7th, 2008
11:00 AM
#2

Re: Match between the arrays
This should get you started on the path to comparing your arrays. Let us know if you get stuck.

Regards,

Jeff

Reply
nithinpes's Avatar
nithinpes
Expert
291 Posts
May 7th, 2008
11:47 AM
#3

Re: Match between the arrays
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;

Last edited by nithinpes : May 7th, 2008 at 11:49 AM. Reason: edited text
Reply
MyMarlboro's Avatar
MyMarlboro
Member
32 Posts
May 7th, 2008
02:40 PM
#4

Re: Match between the arrays
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.

Reply
nithinpes's Avatar
nithinpes
Expert
291 Posts
May 8th, 2008
04:16 AM
#5

Re: Match between the arrays
Quote:
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.

Reply
nithinpes's Avatar
nithinpes
Expert
291 Posts
May 8th, 2008
04:28 AM
#6

Re: Match between the arrays
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.  

Reply
MyMarlboro's Avatar
MyMarlboro
Member
32 Posts
May 9th, 2008
06:02 AM
#7

Re: Match between the arrays
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.

Reply
nithinpes's Avatar
nithinpes
Expert
291 Posts
May 9th, 2008
09:49 AM
#8

Re: Match between the arrays
Quote:
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

Reply
nithinpes's Avatar
nithinpes
Expert
291 Posts
May 9th, 2008
10:45 AM
#9

Re: Match between the arrays
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;

Reply
MyMarlboro's Avatar
MyMarlboro
Member
32 Posts
May 9th, 2008
02:55 PM
#10

Re: Match between the arrays
Hi nithinpes,
THANKS you so much for your continuous guidance and help.
I learnt a lot from you.
Thank you & God Bless.

:)

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,872 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Perl Forum Contributors