in reply to Re^2: How to compare two associative arrays efficiently
in thread How to compare two associative arrays efficiently
Please use <c>...</c> around your code.
Forget effeciency, your code doesn't even work. If both hashes are initialized to (a=>1, b=>2, c=>3), you end up with qw( b c a c a b 2 3 1 3 1 2 ) in @array instead of nothing.
Update Ignore the remainder of this post based on new information provided in another post.
The fix (assuming the values are strings) is below, although it's very weird that you want to merge the keys and the values.
foreach my $key (keys %pssn) { if (not exists $assn{$key}) { push(@array, $key); } } foreach my $key (keys %assn) { if (not exists $pssn{$key}) { push(@array, $key); } } my %assn_vals = map { $_ => 1 } values %assn; my %pssn_vals = map { $_ => 1 } values %pssn; foreach my $val (keys %assn_vals) { if (not exists $pssn_vals{$val}) { push(@array, $val); } } foreach my $val (keys %pssn_vals) { if (not exists $assn_vals{$val}) { push(@array, $val); } }
|
|---|