Reputation: 11
I am trying to do a simple comparison of rows of two seperate name-value arrays in Perl (5.8.8), but can not find what I'm doing wrong. I seems that within a for keys
I have declared the two arrays:
my %array1;
my %array2;
I fill up the rows like this, to build name-value rows:
$array1{$name}=$value;
and array two contains rows with the same name (but possibly different values):
$array2{$name}=$value2;
Now, I compare the different values for every row in array 1 like this:
for my $k(keys %array1) {
if ($array1{$k} ne $array2{$k}) {
.. more program logic
}
But, it seems the second array is not defined within the for loop running over the 'keys' of the first array (if I insert a unless (defined $array2($k)) this hits, and when trying to use the $array2(k) anyhow, I get the error 'Use of uninitialized value'. I'm lost here. Why doesn't this work?
Upvotes: 0
Views: 135
Reputation: 11
Hashes, array of name-value pairs, associative arrays, yes. So I have implemented a hash-on-hash 2-dimensional associative array to keep everything within one 'structure', and all works fine. Still don't get what was wrong in the two seperate hashes approach, must have been some typo, but hey problem solved. Thanks for the input.
Upvotes: 1
Reputation: 91428
You could test for existence of key in %array2, like this:
for my $k(keys %array1) {
if (!exists($array2{$k}) || $array1{$k} ne $array2{$k}) {
.. more program logic
}
Upvotes: 0