Hi Perl monks
I am new to perl and was hoping you could possibly help me with how best to go about doing the following. I have tried many different ways and I just can't seem to get this working.
I have a very large file which consists of numerous hashes of hashes and I need to parse this data structure using a defined schema file and then output all the values in the order defined within the schema.
Below is a very small sample of the file containing the hashes of hashes that I need to parse. I need to be able to parse this without hard coding any of the hash names (@dev, @fred etc). The original file has about 50 more hashes of hashes defined.
{
"schema":"Configuration file for our servers",
"description":"mount points for servers",
"@dev":{
"home_nfs":{
"%default":{
"%mount_opts":"nfsvers=3,timeo=600,retrans=2",
"%mount_user":"root",
"%mount_group":"root",
"%mount_acl":"0755"
},
"home-lnk-mpt":{
"%export_name":"/links10",
"%filer_device":{
"ny_loc":"nydevnfstest10_links",
"nj_loc":"njdevnfstest11_links"
},
"%filer_volume":{
"ny_loc":"/vol/linkstest10",
"nj_loc":"/vol/linkstest11"
}
}
}
},
"@fred":{
"home_nfs":{
"%default":{
"%mount_opts":"nfsvers=3,timeo=600,retrans=2",
"%mount_user":"root",
"%mount_group":"root",
"%mount_acl":"0755"
},
"home-lnk-mpt":{
"%export_name":"/links",
"%filer_device":{
"ny_loc":"nydevnfs_links",
"nj_loc":"njdevnfs_links"
},
"%filer_volume":{
"ny_loc":"/vol/links",
"nj_loc":"/vol/links"
}
}
}
}
}
I have the following schema file that defines all the keys that a hash should have (this is the %definition) and then an %output section that defines how the data should be outputted
{
"%name": "nfs mount schema",
"%description": "Definitions for nfs_mount properties and their re
+quired fields",
"%definition": {"nfs": {
"%comment": "Properties for an NFS mount definition",
"%mount_opts": {"%type": "string"},
"%mount_user": {"%type": "string"},
"%mount_group": {"%type": "string"},
"%mount_acl": {"%type": "integer"},
"%export_name": {"%type": "path"},
"%filer_device": {"%type": "string"},
"%filer_volume": {"%type": "path"}
}},
"%output": {
"%comment": "Output format available",
"mountnfs": {
"%comment": "Output format needed for the mount script",
"%uses": "nfs",
"%format": "%tag:%filer_device:%filer_volume:%export_name"
}
}
}
So what I am trying to do is essentially output two files; one for ny_loc and one for nj_loc.
I need to read the file containg all the hashes of hashes and compare each hash with the schema and if the hash has the all the keys that are defined in the schema %definition section then output a line of text to ny_loc file and another line of text to nj_loc file using the output format described in the schema %output section.
And continuing doing the same for every hash encountered and appending lines of text to the files ny_loc and nj_loc.
So for example I want to be able to do something like this.
Read file containing hashes of hashes and then for example I find @dev, @fred, @fred1 etc I then want to search through each of these for anymore hashes. In the small sample I have provided I would then find home_nfs in both @dev and @fred (note in the original file there are loads more hashes defined at this level). Then I want to be able to look at all the hashes defined at this level for example home_nfs etc and then check each and every hash like home-lnk-mpt etc to see whether it has all the keys defined in the schema %definition section and if it has then using the format in the %output section of the schema I want to output a line of text to ny_loc file and a line of text to nj_loc file for each hash that is processed.
So when this particular hash of hashes is parsed I will have two files with this in:
ny_loc file =
@dev:nydevnfstest10_links:/vol/linkstest10:/links10
@fred:nydevnfs_links:/vol/links:/links
nj_loc file =
@dev:njdevnfstest11_links:/vol/linkstest11:/links10
@fred:njdevnfs_links:/vol/links:/links