in reply to Re^2: What is the easiest way to merge two hashes?
in thread What is the easiest way to merge two hashes?
this code can determine the joined set of keys of two hash references given the keys of the two hash references separately
So does this, rather more simply:
sub mergedHashKeys{ my( $r1, $r2 ) = @_; my %h = ( %{ $r1 }, %{ $r2 } ); return [ keys %h ]; };; $hash1 = { common_key => 'foo', hash1_specific_key => 'some value' };; $hash2 = { common_key => 'bar', hash2_specific_key => 'some other value' };; pp mergedHashKeys( $hash1, $hash2 );; ["hash2_specific_key", "common_key", "hash1_specific_key"]
Or if you are into programming in hieroglyphics, just:
sub mergedHashKeys{ [ keys %{{ %{ $_[0] }, %{ $_[1] } }} ] };;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: What is the easiest way to merge two hashes?
by tkguifan (Scribe) on Feb 15, 2015 at 14:30 UTC | |
by BrowserUk (Patriarch) on Feb 15, 2015 at 16:37 UTC |