NAME
Hash::Subset - Produce subset of a hash
VERSION
This document describes version 0.007 of Hash::Subset (from Perl distribution Hash-Subset), released on 2022-07-27.
SYNOPSIS
hash_subset
hashref_subset
hash_subset_without
hashref_subset_without
merge_hash_subset
merge_overwrite_hash_subset
merge_ignore_hash_subset
merge_hash_subset_without
merge_overwrite_hash_subset_without
merge_ignore_hash_subset_without
)
;
# using keys specified in an array
my
%subset
= hash_subset ({
a
=>1,
b
=>2,
c
=>3}, [
'b'
,
'c'
,
'd'
]);
# => (b=>2, c=>3)
my
$subset
= hashref_subset({
a
=>1,
b
=>2,
c
=>3}, [
'b'
,
'c'
,
'd'
]);
# => {b=>2, c=>3}
# using keys specified in another hash
my
%subset
= hash_subset ({
a
=>1,
b
=>2,
c
=>3}, {
b
=>20,
c
=>30,
d
=>40});
# => (b=>2, c=>3)
my
$subset
= hashref_subset({
a
=>1,
b
=>2,
c
=>3}, {
b
=>20,
c
=>30,
d
=>40});
# => {b=>2, c=>3}
# filtering keys using a coderef
my
%subset
= hash_subset ({
a
=>1,
b
=>2,
c
=>3},
sub
{
$_
[0] =~ /[bc]/});
# => (b=>2, c=>3)
my
$subset
= hashref_subset({
a
=>1,
b
=>2,
c
=>3},
sub
{
$_
[0] =~ /[bc]/});
# => {b=>2, c=>3}
# multiple filters: array, hash, coderef
my
%subset
= hash_subset ({
a
=>1,
b
=>2,
c
=>3,
d
=>4}, {
c
=>1}, [
qw/b/
],
sub
{
$_
[0] =~ /[bcd]/});
# => (b=>2, c=>3, d=>4)
my
$subset
= hashref_subset({
a
=>1,
b
=>2,
c
=>3,
d
=>4}, {
c
=>1}, [
qw/b/
],
sub
{
$_
[0] =~ /[bcd]/});
# => {b=>2, c=>3, d=>4}
# excluding keys
my
%subset
= hash_subset_without ({
a
=>1,
b
=>2,
c
=>3}, [
'b'
,
'c'
,
'd'
]);
# => (a=>1)
my
$subset
= hashref_subset_without({
a
=>1,
b
=>2,
c
=>3}, [
'b'
,
'c'
,
'd'
]);
# => {a=>1}
A use case is when you use hash arguments:
sub
func1 {
my
%args
=
@_
;
# known arguments: foo, bar, baz
...
}
sub
func2 {
my
%args
=
@_
;
# known arguments: all func1 arguments as well as qux, quux
# call func1 with all arguments passed to us
my
$res
= func1(hash_subset(\
%args
, [
qw/foo bar baz/
]));
# postprocess result
...
}
If you use Rinci metadata in your code, this will come in handy, for example:
my
%common_args
= (
foo
=> {...},
bar
=> {...},
baz
=> {...},
);
$SPEC
{func1} = {
v
=> 1.1,
args
=> {
%common_args
,
},
};
sub
func1 {
my
%args
=
@_
;
...
}
$SPEC
{func2} = {
v
=> 1.1,
args
=> {
%common_args
,
# func2 supports all func1 arguments plus a couple of others
qux
=> { ... },
quux
=> { ... },
},
};
sub
func2 {
my
%args
=
@_
;
# call func1 with all arguments passed to us
my
$res
= func1(hash_subset(\
%args
,
$SPEC
{func1}{args}));
# postprocess result
...
}
Merging subset to another hash:
my
%target
= (
a
=>1,
b
=>2);
merge_hash_subset(\
%target
, {
foo
=>1,
bar
=>2,
baz
=>3},
qr/ba/
);
# %target becomes (a=>1, b=>2, bar=>2, baz=>3)
merge_hash_subset_without(\
%target
, {
foo
=>1,
bar
=>2,
baz
=>3},
qr/ba/
);
# %target becomes (a=>1, b=>2, foo=>1)
DESCRIPTION
Keywords: hash arguments, hash picking, hash grep, hash filtering, hash merging
FUNCTIONS
None exported by default.
hash_subset
Usage:
my
%subset
= hash_subset (\
%hash
,
@keys_srcs
);
my
$subset
= hashref_subset(\
%hash
,
@keys_srcs
);
Where each @keys_src element can either be an arrayref, a hashref, a Regexp object, or a coderef. Coderef will be called with args($key, $value) and return true when key should be included.
Produce subset of %hash
, returning the subset hash (or hashref, in the case of hashref_subset
function).
Perl lets you produce a hash subset using the hash slice notation:
my
%subset
=
%hash
{
"b"
,
"c"
,
"d"
};
The difference with hash_subset
is: 1) hash slice is only available since perl 5.20 (in previous versions, only array slice is available); 2) when the key does not exist in the array, perl will create it for you with undef
as the value:
my
%hash
= (
a
=>1,
b
=>2,
c
=>3);
my
%subset
=
%hash
{
"b"
,
"c"
,
"d"
};
# => (b=>2, c=>3, d=>undef)
So basically hash_subset
is equivalent to:
my
%subset
=
%hash
{
grep
{
exists
$hash
{
$_
}}
"b"
,
"c"
,
"d"
};
# => (b=>2, c=>3)
and available for perl earlier than 5.20. In addition to that, hash_subset() accepts arrayref & Regexp object as well as hashref/coderef, and several of them.
hashref_subset
See "hash_subset".
hash_subset_without
Like "hash_subset", but reverses the logic: will create subset that only includes keys not in the specified arrays/hashes/Regexps/coderefs.
hashref_subset_without
merge_hash_subset
Usage:
merge_hash_subset (\
%h1
, \
%h2
,
@keys_src
);
merge_overwrite_hash_subset(\
%h1
, \
%h2
,
@keys_src
);
merge_ignore_hash_subset (\
%h1
, \
%h2
,
@keys_src
);
merge_hash_subset
selects a subset of hash %h2
(using @keys_src
, just like in "hash_subset") and merge the subset to hash %h1
. This is basically a convenience shortcut for:
my
%subset
= hash_subset(\
%h2
,
@keys_src
);
for
my
$key
(
keys
%subset
) {
die
"Duplicate key when merging subset: $key"
if
exists
$h1
{
$key
];
$h1
{
$key
} =
$subset
{
$key
};
}
while merge_overwrite_hash_subset
does something like this:
my
%subset
= hash_subset(\
%h2
,
@keys_src
);
for
my
$key
(
keys
%subset
) {
$h1
{
$key
} =
$subset
{
$key
};
}
and merge_ignore_hash_subset
does something like this:
my
%subset
= hash_subset(\
%h2
,
@keys_src
);
for
my
$key
(
keys
%subset
) {
next
if
exists
$h1
{
$key
};
$h1
{
$key
} =
$subset
{
$key
};
}
merge_overwrite_hash_subset
See "merge_hash_subset".
merge_ignore_hash_subset
See "merge_hash_subset".
merge_hash_subset_without
Usage:
merge_hash_subset_without (\
%h1
, \
%h2
,
@keys_src
);
merge_overwrite_hash_subset_without(\
%h1
, \
%h2
,
@keys_src
);
merge_ignore_hash_subset_without (\
%h1
, \
%h2
,
@keys_src
);
These are like "merge_hash_subset", "merge_overwrite_hash_subset", and "merge_ignore_hash_subset" except these routines will merge subset from %h2
that do not contain keys specified by @keys_src
.
merge_overwrite_hash_subset_without
See "merge_hash_subset_without".
merge_ignore_hash_subset_without
See "merge_hash_subset_without".
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Hash-Subset.
SOURCE
Source repository is at https://github.com/perlancar/perl-Hash-Subset.
SEE ALSO
Hash::MoreUtils provides various ways to create hash subset ("slice") through its slice_*
functions. It does not provide way to specify subset keys via the keys of %another_hash
, but that can be done trivially using keys %another_hash
. Hash::Subset is currently more lightweight than Hash::MoreUtils.
Tie::Subset::Hash to create a tied version of a hash subset (a "view" of a subset of a hash).
Hash::Util::Pick also allows you to create a hash subset by specifying the wanted keys in a list or via filtering using a coderef. This XS module should perhaps be preferred over Hash::Subset for its performance, but there are some cases where you cannot use XS modules.
See some benchmarks in Bencher::Scenarios::HashPicking.
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2022, 2020, 2019 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Hash-Subset
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.