Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    shmem often walks around searching his glasses. In order to have a better sight, I put them on.
    [Discipulus]: I'm advantaged: berrillio is always on my noose
    Discipulus looks for his glasses just to defeat his thirst

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (2)
    As of 2018-05-20 16:29 GMT
    Find Nodes?
      Voting Booth?