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.

In reply to Re^2: Setting a multi-dimensional hash value by Dallaylaen
in thread Setting a multi-dimensional hash value by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.