eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:

Today a workmate showed me the following function to set a hash value (leveraging autovivification):

use strict; use warnings; use Data::Dumper; sub set_hash { my $href = shift; my $val = shift; my @key = @_; $href->{$key[0]}{$key[1]}{$key[2]} = $val; } my %h; set_hash(\%h, 42, 'a', 'b', 'c'); set_hash(\%h, 69, 'x', 'y', 'z'); print Dumper(\%h);
and asked: how can I generalize this function to take any number of hash keys?

Though probably an XY Problem, I tentatively suggested:

use strict; use warnings; use Data::Dumper; sub set_hash { my $href = shift; my $val = shift; my @key = @_; my $s = '$href->'; for my $k (@key) { $s .= "{'" . $k . "'}" } $s .= "='" . $val . "'"; my $rc = eval $s; defined($rc) or die "eval error: $@"; } my %h; set_hash(\%h, 42, 'a', 'b', 'c'); set_hash(\%h, 69, 'x', 'y', 'z'); set_hash(\%h, 'jock', 'shock'); set_hash(\%h, 'value', 'abc', 'def', 'ghi', 'jkl', 'xyz'); print Dumper(\%h);
Running the above program produces:
$VAR1 = { 'abc' => { 'def' => { 'ghi' => { 'jkl' => { 'xyz' => 'value' } } } }, 'x' => { 'y' => { 'z' => '69' } }, 'shock' => 'jock', 'a' => { 'b' => { 'c' => '42' } } };
As you might expect, I was not happy about resorting to string eval for this. How would you do it?

Replies are listed 'Best First'.
Re: Setting a multi-dimensional hash value
by choroba (Cardinal) on Apr 30, 2015 at 11:12 UTC
    Data::Diver.
    #!/usr/bin/perl use warnings; use strict; use Data::Diver qw{ DiveVal }; use Data::Dumper; my %h; DiveVal(\%h, 'shock') = 'jock'; DiveVal(\%h, qw( a b c )) = 42; DiveVal(\%h, qw( x y z )) = 69; DiveVal(\%h, qw( abc def ghi jkl xyz )) = 'value'; print Dumper \%h;

    Note that the result differs slightly from your solution: 42 and 69 are stored as numbers, not strings.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Setting a multi-dimensional hash value
by roboticus (Chancellor) on Apr 30, 2015 at 11:01 UTC

    eyepopslikeamosquito:

    If I were going to, I might do it something like this:

    $ perl tuv.pl $VAR1 = { 'a' => { 'b' => { 'c' => 42 } }, 'abc' => { 'def' => { 'ghi' => { 'jkl' => { 'xyz' => 'value' } } } }, 'shock' => 'jock', 'x' => { 'y' => { 'z' => 69 } } }; Roboticus@Waubli ~ $ cat tuv.pl #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; sub set_hash { my $hr = shift; my $val = shift; my $last_key = pop; while (@_) { my $key = shift; $hr->{$key} = {} if ! exists $hr->{$key}; $hr = $hr->{$key}; } $hr->{$last_key} = $val; } my %h; set_hash(\%h, 42, 'a', 'b', 'c'); set_hash(\%h, 69, 'x', 'y', 'z'); set_hash(\%h, 'jock', 'shock'); set_hash(\%h, 'value', 'abc', 'def', 'ghi', 'jkl', 'xyz'); print Dumper(\%h);

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I believe the loop may be shortened to just this:
      foreach my $key (@_) { $hr = $hr->{$key} ||= {}; };
      I did a quick benchmark and it looks like it's also faster:
      #!/usr/bin/perl -w use strict; use Data::Dumper; use Benchmark qw(cmpthese); my $hash = {}; set_hash( $hash, 42, qw(foo bar baz x y z t) ); print Dumper( $hash ); my $hash_foreach = {}; set_hash_foreach( $hash_foreach, 42, qw(foo bar baz x y z t) ); my $hash_exists = {}; set_hash_exists( $hash_exists, 42, qw(foo bar baz x y z t) ); Dumper($hash_foreach) eq Dumper($hash) or die "hash_foreach inconsiste +nt"; Dumper($hash_exists) eq Dumper($hash) or die "hash_exists inconsistent +"; cmpthese ( -1, { while => sub { for (1..10000) { set_hash( {}, 42, qw(foo bar baz x y z t) ); }; }, foreach => sub { for (1..10000) { set_hash_foreach( {}, 42, qw(foo bar baz x y z t) ); }; }, exists => sub { for (1..10000) { set_hash_exists( {}, 42, qw(foo bar baz x y z t) ); }; }, }); sub set_hash { my $hr = shift; my $val = shift; my $last_key = pop; while (@_) { my $key = shift; $hr->{$key} = {} if ! exists $hr->{$key}; $hr = $hr->{$key}; } $hr->{$last_key} = $val; }; sub set_hash_foreach { my $hash = shift; my $value = shift; my $last_key = pop; foreach my $k (@_) { $hash = $hash->{$k} ||= {}; }; $hash->{$last_key} = $value; }; sub set_hash_exists { my $hr = shift; my $val = shift; my $last_key = pop; foreach my $key (@_) { $hr->{$key} = {} if ! exists $hr->{$key}; $hr = $hr->{$key}; } $hr->{$last_key} = $val; };
      I thought exists would benefit over a ||=, but it looks like it doesn't:
      $VAR1 = { 'foo' => { 'bar' => { 'baz' => { 'x' => { 'y' => { 'z' => { +'t' => 42 } } } } } } }; Rate while exists foreach while 21.3/s -- -10% -21% exists 23.6/s 11% -- -12% foreach 27.0/s 27% 14% --
      Still I ++ your answer.

        Dallaylaen:

        Yes, it certainly looks like it can be shortened. I also also played around with a prototype version so you don't have to explicitly take the reference to the hash:

        #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; sub set_hash(+$@) { my $hr = shift; my $val = shift; my $last_key = pop; while (@_) { my $key = shift; $hr->{$key} = {} if ! exists $hr->{$key}; $hr = $hr->{$key}; } $hr->{$last_key} = $val; } my %h; set_hash(%h, 42, 'a', 'b', 'c'); set_hash(%h, 69, 'x', 0, 'z'); set_hash(%h, 69, 'x', 0, 'y'); set_hash(%h, 'jock', 'shock'); set_hash(%h, 'value', 'abc', 'def', 'ghi', 'jkl', 'xyz'); print Dumper(\%h); my $h2 = {}; set_hash($h2, 42, 'a', 'b', 'c'); set_hash($h2, 69, 'x', 0, 'z'); set_hash($h2, 69, 'x', 0, 'y'); set_hash($h2, 'jock', 'shock'); set_hash($h2, 'value', 'abc', 'def', 'ghi', 'jkl', 'xyz'); print Dumper($h2);

        Update: Repaired typo.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Setting a multi-dimensional hash value
by hdb (Monsignor) on Apr 30, 2015 at 11:09 UTC

    Recursively:

    use strict; use warnings; use Data::Dumper; sub set_hash { my $href = shift; my $val = shift; my $key = shift; if( @_ ) { $href->{$key} = {} if not exists $href->{$key}; set_hash( $href->{$key}, $val, @_ ); } else { $href->{$key} = $val; } } my %h; set_hash(\%h, 42, 'a', 'b', 'c'); set_hash(\%h, 69, 'x', 'y', 'z', 'aa' ); print Dumper(\%h);
Re: Setting a multi-dimensional hash value
by RichardK (Parson) on Apr 30, 2015 at 11:07 UTC

    what's wrong with $h{a}{b}{c} = 42; ? In your example set_hash is just being obscure.

      The idea was to generalize it to any depth, not just 3.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of