Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
OK. I translated your JSON manually to a Perl hash and added a missing else-block to your routine.

Now I got the following output:

%mount_acl = 0755 %mount_group = root %mount_opts = nfsvers=3,timeo=600,retrans=2 %export_name = /links %export_name = /home7 %export_name = /home8

apparently most elements you wanted, just in different lines because you finished your prints with "\n".

IMHO you still need to store the defaults and print them if there are no other details for a path.

Here my code. this should give you a start to continue:

use strict; use warnings; use Data::Dump qw/pp/; my $nfs ={ '%name'=> 'Global Central Configuration file', '%description'=> 'A representation of nfs mount points for d +ev machines', '%schema'=> 'schema.conf', '@dev'=> { 'home_nfs'=> { '%default'=> { '%mount_opts'=> 'nfsve +rs=3,timeo=600,retrans=2', '%mount_user'=> 'root' +, '%mount_group'=> 'root +', '%mount_acl'=> '0755' }, '%comment'=> '----Home NFS Directori +es----', 'home-lnk-mpt'=> { '%export_name'=> ' +/links', '%filer_device'=> +{ + '@west'=> 'nydevnfs_links', + '@ridge'=> 'rnap7750-s' +}, '%filer_volume'=> +{ + '@west'=> '/vol/links', + '@ridge'=> '/vol/links_c' +} }, 'home7_mpt'=> { '%export_name'=> '/ho +me7', '%filer_device'=> { '@ +west'=> 'nydevnfs_home7', '@ +ridge'=> 'rnap7751-s' }, '%filer_volume'=> { '@ +west'=> '/vol/home7', '@ +ridge'=> '/vol/home7_c' } }, 'home8_mpt'=> { '%export_name'=> '/ho +me8', '%filer_device'=> { '@ +west'=> 'nydevnfs_home2', '@ +ridge'=> 'rnap2114-s' }, '%filer_volume'=> { '@ +west'=> '/vol/home2', '@ +ridge'=> '/vol/home2_c' } }, } } }; my @keylist=('%export_name','%filer_device','%filer_volume','%mount_ac +l','%mount_group','%mount_opts'); recurse_hash($nfs,@keylist); sub recurse_hash { my($hash,@findkeys) = @_; foreach (sort keys %{$hash}) { my $value = $hash->{$_}; if (ref($value) eq 'HASH') { recurse_hash($value,@findkeys); } else { for my $key (@findkeys) { if ($key eq $_) { print "$_ = $value\n"; } } } } }

Cheers Rolf

( addicted to the Perl Programming Language)


In reply to Re: Traversing a complex data structure searching for certain keys and printing their values by LanX
in thread Traversing a complex data structure searching for certain keys and printing their values by newperluser2013

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (11)
    As of 2014-12-21 18:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (106 votes), past polls