Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Return a Deep Hash Reference from Array of Keys

by Laurent_R (Vicar)
on Aug 15, 2013 at 09:30 UTC ( #1049537=note: print w/ replies, xml ) Need Help??


in reply to Return a Deep Hash Reference from Array of Keys

What about something like this:

DB<9> @list = qw /wheels four/; DB<10> print Dumper $hash->{shift @list}{shift @list}; $VAR1 = { 'car' => { 'plain' => { 'sporty' => 'honda', 'retro' => 'volkswagon' }, 'fancy' => { 'silver' => 'maserati', 'red' => 'ferrari' } }, 'truck' => { 'light' => { 'chevy' => 'pickup' }, 'utility' => { 'city' => 'trash', 'muni' => 'power', 'us' => 'mail' } } }; DB<11>


Comment on Re: Return a Deep Hash Reference from Array of Keys
Download Code
Re^2: Return a Deep Hash Reference from Array of Keys
by bratwiz (Sexton) on Aug 16, 2013 at 20:38 UTC

    Interesting idea, but it doesn't actually work.

    Here is the output:

    #! /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', 'utility', 'blah'); fetch2($hash, 'wheels', 'four', 'truck', 'utility', 'corp', 'dom'); ## Using your suggested technique sub fetch2 { my ($hash, @keys) = @_; print "==> Fetching [" . join(', ', @keys) . ']' . "\n"; print Dumper $hash->{shift @keys}{shift @keys}; }

    Produces:

    ==> Fetching [wheels] $VAR1 = undef; ==> Fetching [wheels, four, truck, utility, blah] $VAR1 = { '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 p +ower', 'vap' => 'virginia p +ower' } } } }; ==> Fetching [wheels, four, truck, utility, corp, dom] $VAR1 = { '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 p +ower', 'vap' => 'virginia p +ower' } } } };

    Whereas the expected result should be:

    ==> 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, utility, blah] (nothing, because it doesn't exist) ==> Fetching [wheels, four, truck, utility, corp, dom] $VAR1 = 'dominion power';

      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:
        Yes, that does look right to me also.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-07-13 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls