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
- @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
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
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:
-
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 7th, 2008 at 11:49 AM.
Reason: edited text
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.
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.
Re: Match between the arrays
Here it is:
-
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;
-
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.
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
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.
-
@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 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.
:)
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).
|