Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Return a Deep Hash Reference from Array of Keys

by Laurent_R (Prior)
on Aug 16, 2013 at 21:13 UTC ( #1049797=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Return a Deep Hash Reference from Array of Keys
in thread Return a Deep Hash Reference from Array of Keys

The example under the debugger that I gave works only for a two-level hash dereferencing. It has to be adapted for other levels.

I slightly modified the program you made based on my idea so that it takes into account the number of elements in the array of keys, as follows:

#! /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', 'corp' => { 'dom' => 'dominion p +ower', 'vap' => 'virginia p +ower', }, }, }, }, 'one' => 'Unicycle', 'two' => { 'pedals' => 'bicycle', 'motorized' => 'motorcycle', }, 'three' => 'tricycle', } }; ##--------------------------- ## DEEP REFERENCE ##--------------------------- fetch2($hash, 'wheels'); fetch2($hash, 'wheels', 'four', 'truck'); fetch2($hash, 'wheels', 'four', 'truck', 'utility'); fetch2($hash, 'wheels', 'four', 'truck', 'utility', 'blah'); fetch2($hash, 'wheels', 'four', 'truck', 'utility', 'corp', 'dom'); ## Using your suggested technique sub fetch2 { my ($hash, @keys) = @_; print "==> Fetching [" . join(', ', @keys) . ']' . "\n"; my $command_string = 'print Dumper $hash->' . '{shift @keys}' +x scalar @keys; eval "$command_string"; }
And I get the following output, which seems correct to me:
$ perl deep.pl ==> 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', 'corp' => { 'dom' => + 'dominion power', 'vap' => + 'virginia power' } } } } }; ==> Fetching [wheels, four, truck] $VAR1 = { 'light' => { 'chevy' => 'pickup' }, 'utility' => { 'city' => 'trash', 'muni' => 'power', 'us' => 'mail', 'corp' => { 'dom' => 'dominion power', 'vap' => 'virginia power' } } }; ==> Fetching [wheels, four, truck, utility] $VAR1 = { 'city' => 'trash', 'muni' => 'power', 'us' => 'mail', 'corp' => { 'dom' => 'dominion power', 'vap' => 'virginia power' } }; ==> Fetching [wheels, four, truck, utility, blah] $VAR1 = undef; ==> Fetching [wheels, four, truck, utility, corp, dom] $VAR1 = 'dominion power';


Comment on Re^3: Return a Deep Hash Reference from Array of Keys
Select or Download Code
Re^4: Return a Deep Hash Reference from Array of Keys
by bratwiz (Sexton) on Sep 13, 2013 at 18:20 UTC
    Yes, that does look right to me also.
      Then you are entitled to upvote my post. ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (16)
As of 2015-07-01 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (9 votes), past polls