Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Hash References - Is it possible to put reference to nested hash into 1 variable?

Tags:

perl

perl-hash

I have a partially nested hash like the following:

$href = {one=>1, word_counts=>{"the"=>34, "train"=>4} };

and I would like to get the value of $href->{'word_counts'}{'train'}. Is it possible to put the {'word_counts'}{'train'} into a variable, so I can access it by simply calling $href->$variable?

like image 742
jzjzjzjz Avatar asked Dec 11 '25 07:12

jzjzjzjz


1 Answers

No, but you can use Data::Diver to get a value given a list of keys:

my @keys = ('word_counts', 'train');
my $value = Data::Diver::Dive($href, \(@keys));
like image 64
ysth Avatar answered Dec 12 '25 20:12

ysth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!