[Chicago-talk] appending hashes

Steven Lembark lembark at jeeves.wrkhors.com
Tue Nov 4 07:32:36 CST 2003



--On Sunday, November 02, 2003 13:25:10 -0600 Jay Strauss <me at heyjay.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);
>
> push %h, %a; #doesn't work
> %h += %a; #doesn't work
>
> I did it manually like:
> map {$h{$_} = $a{$_}} keys %a;
>
> But, thought there may be a shortcut syntax

Assign the slice. You cannot "push" a hash, however, because they
are not ordered. Any assignment you make from one hash onto anohter
will overwrite existing contents.

Given you want the replacements then:

    @foo{keys %bar} = values %bar;

will do it for you -- with the caveat that existing values in
%foo will be overwritten with values from %bar.

That or:

    exists $foo{$_} or $foo{$_} = $bar{$_} for keys %bar;

will assign only new values to %foo.


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



More information about the Chicago-talk mailing list