P is for Practical | |
PerlMonks |
Merge hashes in specific formatby ovedpo15 (Pilgrim) |
on Jan 12, 2019 at 00:11 UTC ( [id://1228412]=perlquestion: print w/replies, xml ) | Need Help?? |
ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I'm asking your wisdom in solving the following problem: I'm have a list of json files in special format which looks as following: Those 'JSONS' are not so 'JSONS' rather they just the hashes I inserted into a file with the following code: I use the following sub in order to decode each json: Also, the sub which I iterate through the JSON files: But of course it won't work, it been overridden every iteration and also it does not merge. I would like to merge those JSON files into one big hash which contains all the data. Better to explain with an example. If I would like to merge the data I showed at the start and the following data: The merged hash should be; In the 'data' key we will add all the directories and their data. And in the 'total' section we will have all the files and their summed numbers. For now I think that directories are unique and there won't be two hashes which contains the same 'dir' key section, so in that case, we just need to concatenate the 'data'. On the other hand, 'total' section is more difficult because we need to sum the values for the same keys. I was wondering if the JSON:PP has a merge utility. I tried to search through their docs but without any success. If it had a merge utility to merge those hashes it will be great. That issue occurred in a project I work on. Problem is I don't really can use any additional modules - only the standard ones (meaning I can't install any additional modules). It's a shame because there is probably a good module that can do it. But if it's a basic/standard module, I might have it. Anyway, what a good and efficient solution can solve this issue? Maybe to change the input structure to be something else? or maybe to add something into it so it will work efficiently? Each of those hashes will contain 10K+ lines so it will be quite a lot (it will be running on a special machine so don't worry about the memory).
Back to
Seekers of Perl Wisdom
|
|