bratwiz has asked for the wisdom of the Perl Monks concerning the following question:
Greetings Fellow Monks, Monkettes & Monkees...
I was writing a program today and hit upon a Perl question I've never thought about before. Suppose I have some sort of hash structure (such as the one in my example below) and I would like to "reach in" and manipulate one of the nested hashes and that I know the necessary keys and in the correct order, but I only have them in an array form (i.e., qw/level1key level2key.../ etc.) What is the best method to return a reference from the hash using an array of keys?
I have constructed the following example which seems to work acceptably, but my question is: "Is the only and/or best approach to solve the problem?"
(Btw, the hash in the example is completely arbitrary and has no real-world significance)Thanks
Jwhitten
#! /bin/perl use Data::Dumper; my $hash = { 'wheels' => { 'four' => { 'car' => { 'plain' => { 'sporty' => 'honda', 'retro' => 'volkswagon', }, 'fancy' => { 'red' => 'ferrari', 'silver' => 'maserati', }, }, 'truck' => { 'light' => { 'chevy' => 'pickup', }, 'utility' => { 'us' => 'mail', 'city' => 'trash', 'muni' => 'power', }, }, }, 'one' => 'Unicycle', 'two' => { 'pedals' => 'bicycle', 'motorized' => 'motorcycle', }, 'three' => 'tricycle', }, }; ##--------------------------- ## DEEP REFERENCE ##--------------------------- fetch($hash, 'wheels'); fetch($hash, 'wheels', 'one'); fetch($hash, 'wheels', 'four', 'truck', 'utility'); fetch($hash, 'wheels', 'four', 'truck', 'utility', 'blah'); # doesn't +exist sub fetch { my ($hash, @keys) = @_; print "==> Fetching [" . join(', ', @keys) . ']' . "\n"; my $results = deep_reference($hash, @keys); print Dumper($results), "\n\n"; } sub deep_reference { my ($hash, @keys) = @_; my $ref = undef; my $code = '$hash->' . join('->', map { "{$_}" } @keys); return eval $code; }
Produces:
==> Fetching [wheels] $VAR1 = { 'three' => 'tricycle', 'one' => 'Unicycle', 'two' => { 'pedals' => 'bicycle', 'motorized' => 'motorcycle' }, 'four' => { 'car' => { 'plain' => { 'sporty' => 'honda', 'retro' => 'volkswagon' }, 'fancy' => { 'silver' => 'maserati', 'red' => 'ferrari' } }, 'truck' => { 'light' => { 'chevy' => 'pickup' }, 'utility' => { 'city' => 'trash', 'muni' => 'power', 'us' => 'mail' } } } }; ==> Fetching [wheels, one] $VAR1 = 'Unicycle'; ==> Fetching [wheels, four, truck, utility] $VAR1 = { 'city' => 'trash', 'muni' => 'power', 'us' => 'mail' }; ==> Fetching [wheels, four, truck, utility, blah] $VAR1 = undef;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Return a Deep Hash Reference from Array of Keys
by choroba (Cardinal) on Aug 14, 2013 at 21:49 UTC | |
by bratwiz (Sexton) on Aug 15, 2013 at 14:12 UTC | |
|
Re: Return a Deep Hash Reference from Array of Keys
by tangent (Parson) on Aug 14, 2013 at 21:52 UTC | |
|
Re: Return a Deep Hash Reference from Array of Keys
by Laurent_R (Canon) on Aug 15, 2013 at 09:30 UTC | |
by bratwiz (Sexton) on Aug 16, 2013 at 20:38 UTC | |
by Laurent_R (Canon) on Aug 16, 2013 at 21:13 UTC | |
by bratwiz (Sexton) on Sep 13, 2013 at 18:20 UTC | |
by Laurent_R (Canon) on Sep 13, 2013 at 18:43 UTC |