[Chicago-talk] appending hashes

Andy Lester andy at petdance.com
Sun Nov 2 18:32:16 CST 2003


> 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, @_ );
}

xoa

--
Andy Lester
andy at petdance.com, AIM:petdance
http://petdance.com/ http://use.perl.org/~petdance/




More information about the Chicago-talk mailing list