Deep::Hash::Utils - functions for iterating over and working with nested hashes
use Deep::Hash::Utils qw(reach slurp nest deepvalue); my %hash = ( A => { B => { W => 1, X => 2, }, C => { Y => 3, Z => 4, }, } ); while (my @list = reach(\%hash)) { print "@list"; } for my $a (sort {$a->[2] cmp $b->[2]} slurp(\%hash)) { print "@$a"; } my %new_hash = (); nest(\%new_hash,1,2,3,4,5); my $value = deepvalue(\%new_hash,1,2,3,4);
This module provides functions for accessing and modifying values in deeply nested data structures.
reach
reach HASHREF
Iterate over each nested data structure contained in the given hash. Returns an array of each nested key/value set.
Just as each lets you iterate over the keys and values in a single hash, reach provides an iterator over any recursively nested data structures.
each
This helps avoid the need to use layers of nested loops in order to iterate over all entities in nested hashes and arrays.
The reference passed to reach can contain any combination of nested hashes and arrays. Hash keys and values will be ordered in the same manner as when using each, keys, or values.
keys
values
use Deep::Hash::Utils qw(reach slurp nest); $\ = "\n"; my %hash = ( A => { B => { W => 1, X => 2, }, C => { Y => 3, Z => 4, }, } ); while (my @list = reach(\%hash)) { print "@list"; } __END__ Outputs: A C Z 4 A C Y 3 A B W 1 A B X 2
slurp
slurp HASHREF
Returns a list of arrays generated by reach at once. Use this if you want the same result of reach with the ability to sort each layer of keys.
for my $a (sort {$a->[2] cmp $b->[2]} slurp(\%hash)) { print "@$a"; } __END__ Output: A B W 1 A B X 2 A C Y 3 A C Z 4
nest
nest HASHREF, LIST
define nested hash keys with a given list
use Data::Dumper; my %new_hash = (); nest(\%new_hash,1,2,3,4,5); print Dumper \%new_hash; __END__ Output: $VAR1 = { '1' => { '2' => { '3' => { '4' => 5 } } } };
deepvalue
deepvalue HASHREF, LIST
retrieve deeply nested values with a list of keys:
my %new_hash = ( '1' => { '2' => { '3' => { '4' => 5 } } } ); print Dumper deepvalue(\%new_hash,1,2,3,4); print Dumper deepvalue(\%new_hash,1,2); __END__ Output: $VAR1 = 5; $VAR1 = { '3' => { '4' => 5 } };
None by default.
https://github.com/neilbowers/perl-deep-hash-utils
Chris Becker, <clbecker@gmail.com<gt>
Now maintained by Neil Bowers <neilb@cpan.org>
Copyright (C) 2009 by Chris Becker
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.
To install Deep::Hash::Utils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Deep::Hash::Utils
CPAN shell
perl -MCPAN -e shell install Deep::Hash::Utils
For more information on module installation, please visit the detailed CPAN module installation guide.