in reply to How to create nesting hashes given the innermost value and an array of keys?

This functiontakes elements in an array and builds them into keys for a hash:
# Usage: UsesArrayElementsAsKeyElementsInHash( \%hash, $value, @array + ) use Carp; sub UsesArrayElementsAsKeyElementsInHash { my $hash = my $root = ( ref $_[0] ? shift : {} ); my $value = shift; my $last = pop; for my $key ( @_ ) { $hash = ( $hash->{$key} ||= {} ); ref $hash or croak "key = $key\n"; } $hash->{$last} = $value; return $root; }
  • Comment on Re: How to create nesting hashes given the innermost value and an array of keys?
  • Download Code