Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Traversing a complex data structure searching for certain keys and printing their values

by LanX (Canon)
on Apr 29, 2013 at 00:12 UTC ( #1031125=note: print w/ replies, xml ) Need Help??


in reply to Traversing a complex data structure searching for certain keys and printing their values

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)


Comment on Re: Traversing a complex data structure searching for certain keys and printing their values
Select or Download Code
Re^2: Traversing a complex data structure searching for certain keys and printing their values
by newperluser2013 (Novice) on Apr 29, 2013 at 00:53 UTC

    Thanks for your help Rolf I really appreciate it. I am still stuck on printing out the lines like this:

    /links:nydevnfs_links:/vol/links:0755:root:root:nfsvers=3,timeo=600,re +trans=2 /links:rnap7750-s:/vol/links_c:0755:root:root:nfsvers=3,timeo=600,retr +ans=2 /home7:nydevnfs_home7:/vol/home7:0755:root:root:nfsvers=3,timeo=600,re +trans=2 /home7:rnap7751-s:/vol/home7_c:0755:root:root:nfsvers=3,timeo=600,retr +ans=2 /home8:nydevnfs_home2:/vol/home2:0755:root:root:nfsvers=3,timeo=600,re +trans=2 /home8:rnap2114-s:/vol/home2_c:0755:root:root:nfsvers=3,timeo=600,retr +ans=2

    please can you help me?

      > please can you help me?

      Sorry, we are giving assistance to learn Perl and not in doing their job without understanding.

      If you really wrote the code you provided, you should be also able to (or at least learn to) modify it!

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-08-20 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls