in reply to condense conditional

Instead of using a conditional, you could just store things in iteratively deeper hashes until you run out of elements in @data:

sub store_data { my( $root, $value, @key ) = @_; while( @key > 1 ) { $k = shift @key; $root->{ $k } ||= {}; $root = $root->{ $k }; }; $root->{ $key[0] } = $value; } store_data( $devstats, $value, @data );

Update: Code was missing a line

Replies are listed 'Best First'.
Re^2: condense conditional
by hippo (Archbishop) on Sep 12, 2018 at 13:31 UTC

    How does this work, given that you are never changing @key and you never set $k? And doesn't $root = $root->{ $k }; just dereference rather than move the root?

    Update: to answer my own questions - the previously missing line allows for the changes and the root moves because of the explicit creation of the sub-hashref. Good solution (++).