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:
$VAR1 = { 'data' => { 'dir1' => { 'fileA' => { 'pid' => { '61781' => 1 }, 'total' => 13 }, 'fileB' => { 'pid' => { '61799' => 1 }, 'total' => 12 } } 'dir2' => { 'fileC' => { 'pid' => { '12345' => 1 }, 'total' => 10 }, 'fileA' => { 'pid' => { '61439' => 1 }, 'total' => 5 } } }, 'total' => { 'fileA' => 18, 'fileB' => 12 'fileC' => 10 } };
Those 'JSONS' are not so 'JSONS' rather they just the hashes I inserted into a file with the following code:
sub encode { my ($path,$href) = @_; my @json_arr = JSON::PP->new->encode($href); return convert_file_to_arr($path,@json_arr); }
I use the following sub in order to decode each json:
sub decode { my ($path,$href) = @_; unless (-e $path) { return 0; } my ($json_data,@jarr); convert_file_to_arr($path,\@jarr); # inserts lines as elements of +array $json_data = (join "",@jarr); %{$href} = %{JSON::PP->new->decode($json_data)}; return 1; }
Also, the sub which I iterate through the JSON files:
sub merge_files_and_exec { my ($path,$list_of_dirs,$data_href) = @_; foreach my $dir (@{$list_of_dirs}) { decode($path,$data_href); } }
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:
$VAR1 = { 'data' => { 'dir3' => { 'fileA' => { 'pid' => { '616161' => 1 }, 'total' => 6 }, 'fileD' => { 'pid' => { '54321' => 1 }, 'total' => 12 } } 'dir4' => { 'fileE' => { 'pid' => { '15151' => 1 }, 'total' => 3 }, 'fileA' => { 'pid' => { '1718' => 1 }, 'total' => 2 } } }, 'total' => { 'fileA' => 8, 'fileD' => 12 'fileE' => 3 } };
The merged hash should be;
$VAR1 = { 'data' => { 'dir1' => { 'fileA' => { 'pid' => { '61781' => 1 }, 'total' => 13 }, 'fileB' => { 'pid' => { '61799' => 1 }, 'total' => 12 } } 'dir2' => { 'fileC' => { 'pid' => { '12345' => 1 }, 'total' => 10 }, 'fileA' => { 'pid' => { '61439' => 1 }, 'total' => 5 } } }, 'dir3' => { 'fileA' => { 'pid' => { '616161' => 1 }, 'total' => 6 }, 'fileD' => { 'pid' => { '54321' => 1 }, 'total' => 12 } } 'dir4' => { 'fileE' => { 'pid' => { '15151' => 1 }, 'total' => 3 }, 'fileA' => { 'pid' => { '1718' => 1 }, 'total' => 2 } } }, 'total' => { 'fileA' => 26, 'fileB' => 12 'fileC' => 10 'fileD' => 12 'fileE' => 3 } };
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).

In reply to Merge hashes in specific format by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.