Merging Hash

Cyber jacked ya, eh?

Merge Hash

sub merge_hash {
    my ($self, $precedent, $subordinate) = @_;
    my @not = grep !exists $precedent->{$_}, keys %{$subordinate};
    @{$precedent}{@not} = @{$subordinate}{@not};
    return $precedent;


I'm not sure I get it.

nevermind, got it. Can't call it as a method ($self) when used as a function. Oops. Anyway if you prototype as (\%\%) as a function then you can call it on two hashes. Or just ignore me. Thats probably best.

I am not sure that i uderstand your idea...
merge_hash takes \%precedent by reference and it gets changed + returned. Overkill?

As i know, the best way to merge hashes is:
my %merged = %precedent, %subordinate;

I never like calling this a merge because some stuff gets left out or overwritten. I think a merge should promote values to array references with both values for shared keys. We should come up with some other term for this, like update.

Most often I use this to set defaults, then overlay that with specified answers. The new layer hides anything in common, which is the point.

It was hard to get whether it's a joke or an invention :))

@brian: collide? :) Anyway, I think sometimes merge does overwrite/leave out data, particularly conflicting ones.

Leave a comment

About mateu

user-pic I blog about Perl.