in reply to Setting a multi-dimensional hash value
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Setting a multi-dimensional hash value
by Dallaylaen (Chaplain) on Apr 30, 2015 at 12:14 UTC | |
by roboticus (Chancellor) on Apr 30, 2015 at 13:05 UTC |