Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Iterate over a perl nested hash data structure

by aitap (Deacon)
on Feb 20, 2013 at 13:55 UTC ( #1019783=note: print w/replies, xml ) Need Help??

in reply to Iterate over a perl nested hash data structure

All you need is love Data::Dumper (pointed out above) and perlreftut, it contains the neseccary instructions for building and accessing multi-level data structures.
Sorry if my advice was wrong.
  • Comment on Re: Iterate over a perl nested hash data structure

Replies are listed 'Best First'.
Re^2: Iterate over a perl nested hash data structure
by NewLondonPerl1 (Acolyte) on Feb 20, 2013 at 15:44 UTC

    So for instance I want to read the hash and do something like this. If find hash called 'bigstor_nfs' then print out to another file all the key/value pairs. then carry on to the next hash name at this level in the data structure. If thats called 'test_nfs' then print out all the key/values pairs to the same output file and then continue doing this comparing the names of each hash until the end of the data structure I think this is possible. I have tried so may different ways and I cant get this to work. Please can you help me?

      As far as I understand the JSON syntax, your document contains only one array (under 'dev' key) which consists of only one hash (with only one key called 'nfsmount'). All other data structures are hashes, which only allow unique key names.

      Thus, you need to print only two hash keys and there is no need in manual listing of all keys in this document. Try this:

      use JSON; use Data::Dumper; ...; # open files, read the JSON string my $data = decode_json $your_json_string; for (qw/bigstor_nfs test_nfs/) { # list of keys to search for print {$filehandle} Dumper { $_ => $data->{tag}{dev}[0]{nfsmount}{$_} + } if exists $data->{tag}{dev}[0]{nfsmount}{$_}; # print to prevousely opened filehandle: # the results of "dumping" of anonymous hash reference # containing the key and its value # if this key exists in the main hash }

      Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019783]
[LanX]: ?
[Corion]: No, or I don't remember the story ;)
LanX my favourite don't tell me the proof of concept is good enough for production anecdote
[ambrus]: ah, it's one of thos
[Eily]: what, there's a difference between proof of concept and production?
[LanX]: 20 years ago traders were complaining about the latency of the trading system...
[ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
[ambrus]: ething that's both cleanly organized and mostly automated.
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2017-03-29 11:47 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.