473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
9 1420
numberwhun
3,509 Expert Mod 2GB
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
410 Expert 256MB
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
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
410 Expert 256MB
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
410 Expert 256MB
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
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
410 Expert 256MB
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
410 Expert 256MB
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
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

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

Similar topics

1
by: fartsniff | last post by:
i found this code out in the ng, and its seems long and clunky, i am still experimenting with preg_match and _replace, but the syntax is a bit confusing. it seems i always misplace or mistype...
20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
3
by: Timmy | last post by:
I'm working on a simple click-through image gallery and I have images with captions in two arrays like this: var current_value="0"; var images = new Array ("photo01.jpg", "photo02.jpg",...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
2
by: stealth_spoof | last post by:
Hi People wondering if anyone can help me with a problem I'm having I'm trying to create an array with an unspecified length, the length is based on the result i get from another task in the code...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
0
by: fariba123 | last post by:
there are two arrays: $players = array( "23" => "Michael Jordan", "32" => "Michael Johnson" ); $current_player = array( "player_id" => "23", "age" => "22"
10
by: somebody | last post by:
There are two files below named search.c and search.h. In the for loop in search.c, the for loop never exits, even if mystruct.field1 has no match. Instead of exiting the for loop it keeps going...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.