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
+ 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 @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 May 7 '08 #1
Share this Question
9 Replies

 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

 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 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;   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

 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

 Expert 100+ P: 410 Here it is: Expand|Select|Wrap|Line Numbers 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;     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

 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

 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 @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;   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.