Combining multiple hash references into one hash reference
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
.
Relevant Pages
- Re: Million hash comparisons
... In a Perl script of mine I have to compare 2 8M-10M ... You mean you compute MD5 hashes of the files ... Maybe the %hash grows too big to fit in memory together with the ... (perl.beginners) - Re: Hashes are good, but not good enough.
... GM> the problem with hashes is that they consume too much memory on big trees, ... GM> and they are too slow for hundred millions of keys. ... IZ> "hundreds of millions of keys", why would not I use hash? ... (comp.lang.perl.misc) - Performance Improvement of complex data structure (hash of hashes of hashes)
... until the last row of the original file is read. ... structure I'm using is a hash of hashes of hashes that stores this ... There are several gigabytes of memory, ... I also tried setting the initial size of each hash using ... (comp.lang.perl.misc) - Re: Million hash comparisons
... In a Perl script of mine I have to compare 2 8M-10M ... You mean you compute MD5 hashes of the files ... Maybe the %hash grows too big to fit in memory together with the ... (perl.beginners) - Re: Handeling huge hashes.
... Store the bulk on disk or in a DB and use several smaller hashes to do the ... now I want to with some logic merge these into a single hash. ... And can depending on the machine simply run out of memory. ... (perl.beginners) |
|