A
Arvin Portlock
I know lots of ways to combine multiple hashes into a single
hash but I'm very concerned about memory and copy by value.
I'm processing some XML documents and have several thousand
elements that must be linked to relatively few hashes. These
hashes have unique keys among them so I don't have to worry
about one hash element overwriting another with the same
key. The following works as it should:
my $hash1 = {
'key1' => 'Value 1',
'key2' => 'Value 2',
'key3' => 'Value 3',
};
my $hash2 = {
'key4' => 'Value 4',
'key5' => 'Value 5',
'key6' => 'Value 6',
};
my %newhash = (%$hash1, %$hash2);
# The following do not work:
# my $newhash = { $hash1, $hash2 };
# my $newhash = [ $hash1, $hash2 ];
# my %newhash = ( $hash1, $hash2 );
foreach my $key (keys %newhash) {
print "$key: $newhash{$key}\n";
}
But I'm concerned I'm creating copies of each of
these elements for all of the thousands of instances
of %newhash I will be creating. Is there a faster and
memory efficient way to do this?
Thanks!
Arvin
hash but I'm very concerned about memory and copy by value.
I'm processing some XML documents and have several thousand
elements that must be linked to relatively few hashes. These
hashes have unique keys among them so I don't have to worry
about one hash element overwriting another with the same
key. The following works as it should:
my $hash1 = {
'key1' => 'Value 1',
'key2' => 'Value 2',
'key3' => 'Value 3',
};
my $hash2 = {
'key4' => 'Value 4',
'key5' => 'Value 5',
'key6' => 'Value 6',
};
my %newhash = (%$hash1, %$hash2);
# The following do not work:
# my $newhash = { $hash1, $hash2 };
# my $newhash = [ $hash1, $hash2 ];
# my %newhash = ( $hash1, $hash2 );
foreach my $key (keys %newhash) {
print "$key: $newhash{$key}\n";
}
But I'm concerned I'm creating copies of each of
these elements for all of the thousands of instances
of %newhash I will be creating. Is there a faster and
memory efficient way to do this?
Thanks!
Arvin