Clear questions and runnable code
get the best and fastest answer
Traversing a complex data structure searching for certain keys and printing their valuesby newperluser2013 (Novice)
|on Apr 28, 2013 at 22:46 UTC||Need Help??|
newperluser2013 has asked for the
wisdom of the Perl Monks concerning the following question:
Hi Monks I am very new to perl and was hoping you could help me with how to do the following. I am trying to recursively traverse a rather large complex data structure which is a hash of hashes. While I am traversing this data structure I want to search for keys with a certain name and then once I have all the keys which make up one line of output I then want to print that line with all the corresponding values. I want to then keep traversing through this data structure and then find the next line of keys that match the keylist I am looking for and then print out the whole line of output. I want to keep doing this until I reach the bottom of the data structure. So I have the following hash of hashes with the keys %export_name, %filer_device, %filer_volume, %mount_acl, %mount_group, %mount_opts, and %mount_user. I am able to parse this and grab all these keys and put them into a list called @keylist.
Now what I have is this really big complex data structure which I want to recursively parse and look for the existance of all the keys in @keylist and print the values. I need to find all of the keys; %export_name, %filer_device, %filer_volume, %mount_acl, %mount_group, %mount_opts, and %mount_user before I can print a line of output as all these are needed to fully define an nfs mount point. There are hundreds of nfs mount points defined in this data structure and I need to print out a line of output in the following format for each and every one thats found: %export_name:%filer_device:%filer_volume:%mount_acl:%mount_group:%mount_user:%mount_opts This is a small sample of this very large data structure I am trying to parse:
The following is my attempt of writing a recursive sub routine to parse this large data structure but I just cant work out how to get it to do what I want
I am calling this subroutine like this but I can't get it to accept passing @keylist to it.
If I hardcode the key names like so it doesn't error and prints all all the corresponding values but they are all on different lines and don't match up.
I am not allowed to hardcode the key names as args to this subroutine. I need to pass in a list of keys defined in @keylist
Using the small sample of this large data structure I have shown I am trying to print lines in the following format.
I mustn't hardcode any references to any mount point names, filer device/volumes names etc. I purely have to parse this data structure and at each level look to see if any of the keys in @keylist exist if they do then store the value until I have values for each key in that list and can then print a line of output, and then continue doing the same throughout parsing the rest of the data structure printing a line of output for each mount point.
Please can you help me with the above? I have spent so much time trying to do this but I just cant seem to get it to work