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;

In reply to Return a Deep Hash Reference from Array of Keys by bratwiz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.