Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

joining 2+ hashes/associate arrays

Status
Not open for further replies.

lleemon

Programmer
Mar 26, 2002
21
GB
Sorry if this is too simple of a question but, if I have two associate arrays how can I combine them into one?

%hash1 = (
'key1', 'value1',
'key2', 'value2',
);

%hash2 = (
'key3', 'value3',
'key4', 'value4',
)

%mainhash = ???

Or can I just add one hash to another?

Thanks for the help in advance.
 
From the Perl Cookbook by Tom Christiansen and Nathan Torkington. Recipe 5.10 Merging Hashes:
Code:
%merged = (%A,%B);
Or a more memory efficient method:
Code:
%merged = ();
while( ($k,$v) = each(%A) ) {
    $merged{$k} = $v;
}
while( ($k,$v) = each(%B) ) {
    $merged{$k} = $v;
}
Cheers, Neil
 
%merged = (%A,%B);

neat....
Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top