I see. OK, that answers one question, but it is not clear from what you have posted so far what constitutes a "test". Is a 'test" the same as a "file"? You still haven't posted any sample data with the expected output from that data. That would be helpful in trying to understand your situation.

Also, are you aware of how fragile it is to try to handle comma-separated values manually by just splitting? Use Text::CSV! (In fact you could encapsulate this whole program into a Text::CSV after_parse callback, but that's a different story...)

Assuming that one file is one test, testing one href, and that you do *not* want multiple hashes each keyed with 'test', but just one, with one listing for each group, maybe you could use something like:

use strict; use warnings; use feature 'say'; use JSON; my $results = {}; for my $href ('root') { my $by_group = {}; while (my $line = <DATA>) { my ( $key, $group, $value, $version, $file, $count ) = split(/ +,/, $line); push @{ $by_group->{ $group } }, $value; } my $test = []; for my $group ( keys %{ $by_group } ) { push @{ $test }, { group => $group, values => $by_group->{ $gr +oup } } } $results->{ $href } = $test; } say JSON->new->pretty->canonical->encode($results); __DATA__ bla,ABC,6.13.00,bla,bla,bla bla,XYZ,1234,bla,bla,bla bla,XYZ,tcsh,bla,bla,bla bla,WEA,6.13.00,bla,bla,bla bla,BAB,ASDAS,bla,bla,bla bla,BAB,12312321,bla,bla,bla bla,SADA,6.13.00,bla,bla,bla bla,SADA,12312321,bla,bla,bla
(^^ that's an SSCCE ...)

$ perl oved.pl { "root" : [ { "group" : "BAB", "values" : [ "ASDAS", "12312321" ] }, { "group" : "ABC", "values" : [ "6.13.00" ] }, { "group" : "XYZ", "values" : [ "1234", "tcsh" ] }, { "group" : "WEA", "values" : [ "6.13.00" ] }, { "group" : "SADA", "values" : [ "6.13.00", "12312321" ] } ] }

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re^3: Trying to make the code more clear and clean by 1nickt
in thread Trying to make the code more clear and clean 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.