[Chicago-talk] appending hashes

Steven Lembark lembark at jeeves.wrkhors.com
Tue Nov 4 07:34:33 CST 2003



--On Sunday, November 02, 2003 18:32:16 -0600 Andy Lester 
<andy at petdance.com> wrote:

>> Is there any shortcut (like a slice or something) to push on hash onto
>> another:
>>
>> my %h = (a=>1, b=>2, c=>3);
>> my %a = (d=>4, e=>5);
>
> %h = (%h,%a);
>
> That turns %h into a list, and %a into a list, and constructs a new hash
> in %h.  Any keys in %h that exist in %a will be overwritten.
>
> This is a common trick to handle parms passed into arrays, especially
> defaults:
>
> sub foo {
> 	my %defaults = (
> 		name => "",
> 		x => 0,
> 		y => 0,
> 	);
>
> 	my %parms = ( %defaults, @_ );
> }

This can get hairy depending on the hash sizes. Flattening both
hashes out and re-assigning them uses up a lot of scratch space.
Assigning via keys/values at least doesn't double the space.

-- 
Steven Lembark                                            2930 W. Palmer
Workhorse Computing                                    Chicago, IL 60647
                                                         +1 888 910 1206



More information about the Chicago-talk mailing list