7

I've dynamic nested hash-refs like this:

my $hash = { 'a' => { 'b' => { 'c' => 'value' } } };

I want to set the value of c to 'something' by allowing the user to input "a.b.c something".

Now getting the value could be done like this:

my $keys = 'a.b.c'; 
my $v='something';
my $h = $hash;
foreach my $k(split /\./, $keys) {
  $h = $h->{$k};
}
print $h; # "value"

But how would I set the value of key c to $v so that

print Dumper $hash;

would reflect the change? $h is not a ref at the end of the foreach loop, so changing that won't reflect the change in $hash. Any hints how to solve the knots in my head?

agranig
  • 129
  • 1
  • 7
  • 2
    Try using the CPAN `Data::Dump` module’s `dd` function instead of the standard `Data::Dumper`’s `Dumper`. The CPAN module makes for much easier reading. – tchrist Jun 10 '12 at 04:56

3 Answers3

6

Something like this:

my $h = $hash;
my @split_key = split /\./, $keys;
my $last_key = pop @split_key;
foreach my $k (@split_key) {
    $h = $h->{$k};
}
$h->{$last_key} = $v;
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
5
sub dive_val :lvalue {
   my $p = \shift;
   $p = \( ($$p)->{$_} ) for @_;
   return $$p;
}

my $data;
my $key = 'a.b.c';
my $val = 'value';

dive_val($data, split /\./, $key) = $val;

A more powerful (and thus slightly harder to use) version of this function is provided by Data::Diver.

use Data::Diver qw( DiveVal );

my $data;
my $key = 'a.b.c';
my $val = 'value';

DiveVal($data //= {}, map \$_, split /\./, $key) = $val;

(daxim's usage is slightly off.)

ikegami
  • 367,544
  • 15
  • 269
  • 518
3
use strictures;
use Data::Diver qw(DiveVal);

my ($hash, $path, $value) = (
    { 'a' => { 'b' => { 'c' => 'value' } } },
    'a.b.c',
    'something',
);

DiveVal($hash, split /[.]/, $path) = $value;
# { a => { b => { c => 'something' } } }
daxim
  • 39,270
  • 4
  • 65
  • 132