Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string "a.b.c" to $hash->{a}->{b}->{c} in Perl

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?

like image 849
agranig Avatar asked Jun 09 '12 22:06

agranig


People also ask

What does =~ mean in Perl?

=~ is the Perl binding operator. It's generally used to apply a regular expression to a string; for instance, to test if a string matches a pattern: if ($string =~ m/pattern/) {

How do I pass a hash to a function in Perl?

To pass a hash, use this: To print a hash reference, use this: PrintAA("test", %$ref_to_hash);

How do I print a hash in Perl?

print "$ perl_print_hash_variable{'-hash_key2'} \n"; Description: The Perl print hash can used $ symbol for a single hash key and its value. The Perl print hash can use the % symbol for multiple hash keys and their values.

How do I dereference a hash in Perl?

In order to dereference, we use the prefix $, @, % or & depending on the type of the variable(a reference can point to a array, scalar, or hash etc).


3 Answers

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;
like image 108
CB Bailey Avatar answered Oct 06 '22 12:10

CB Bailey


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.)

like image 44
ikegami Avatar answered Oct 06 '22 12:10

ikegami


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' } } }
like image 44
daxim Avatar answered Oct 06 '22 13:10

daxim