Hash::Helper - Common hash manipulation functions
version 0.001
use Hash::Helper qw(:verbose);
my %hash = defined_subset( \%other_hash );
my %hash = defined_slice( \%other_hash, qw(key1 key2) );
my %hash = exists_slice( \%other_hash, qw(key1 key2) );
my %hash = transform_slice( \%other_hash, firstname => "FirstName" );
my %hash = transform_defined_slice( \%other_hash, firstname => "FirstName" );
my %hash = transform_exists_slice( \%other_hash, firstname => "FirstName" );
I keep rewriting the same functions to manipulate hashes. You probably do, as well.
When this was a problem for arrays, we got List::Util, but there doesn't seem to be an equivalent for hashes. (Note that Hash::Util exists, but it contains much more specialised functions than these.)
This module is just a container for a few functions we keep rewriting.
Nothing is exported by default because that's naughty.
Instead, each function can be explicitly requested on the import line.
You can also use the tags :verbose or :short to import the long or
short forms of all functions.
Each function below lists first its verbose name, then its short name.
my %new_hash = defined_subset( \%old_hash );
Returns a key-value pair list of the subset of the input hash whose values were defined.
Exactly equivalent to:
map { defined $oldhash{$_} ? $_ => $oldhash{$_} : () } keys %oldhash
my %new_hash = defined_slice( \%old_hash, qw( key1 key2 ) );
Returns a slice of the input hash, using the given keys, only where the values in the input hash are defined. Using standard Perl rules, nonexistent keys are undefined.
You could also write this as
defined_subset( { %old_hash{qw(key1 key2)} } )
but that's a bit more verbose and does extra operations. (In fact,
defined_subset is written in terms of defined_slice.)
my %new_hash = exists_slice( \%old_hash, qw( key1 key2 ) );
Returns a slice of the input hash, using the given keys, only where those keys exist in the input hash.
my %new_hash = transform_slice( \%old_hash, oldkey => 'newkey' );
Returns a slice of the input hash with transformed keys. Note that the keys are a key-value list, not a hashref. The key from the transform list is used against the input hash, and the corresponding value is used as the key in the returned hash.
my %new_hash = transform_defined_slice( \%old_hash, oldkey => 'newkey' );
Exactly as transform_slice, except only those values that are defined
in the input hash are included (under their new key) in the output
hash.
my %new_hash = transform_exists_slice( \%old_hash, oldkey => 'newkey' );
Exactly as transform_slice, except only those keys that exist in the
input hash are included (under their new name) in the output hash.
Alastair Douglas [email protected]
This software is Copyright (c) 2023 by Alastair Douglas.
This is free software, licensed under:
The MIT (X11) License