in reply to Re^3: Size of a HASH (with more keys)
in thread Size of a HASH (with more keys)

In as hash I have stored several values, that have different Keys, but are in same Hash. Example:
$testHash{$year}{$status} = $getNumbers; -> $testHash{2014}{Open} = 5; -> $testHash{2014}{Closed} = 2; -> $testHash{2014}{Cancled} = 3; etc.
Everything is inside one Hash called %testHash.

I posted a LINK above to where you can see how JSONP file format should look like. That is why I need commas inbetween sentences, BUT not at the last sentence.

I use like this:

foreach $key (keys %testHASH){ foreach $key2 (keys %{$testHASH{key}}{ $getNumbersOpen = $testHASH{$year}{$status}{opened}; $getNumbersClosed = $testHASH{$year}{$status}{closed}; $line = "'$status' : {'Open: $getNumbersOpen, 'Closed': $getNumbersClo +sed},\n; ----> If loop to remove the comma <---- print $outfile "$line\n"; } }

I did not copy/paste the code, so there might be silly syntax errors in there.

Replies are listed 'Best First'.
Re^5: Size of a HASH (with more keys)
by Corion (Patriarch) on Jul 02, 2014 at 13:02 UTC
Re^5: Size of a HASH (with more keys)
by AnomalousMonk (Archbishop) on Jul 02, 2014 at 13:29 UTC

    Looking at your pseudocode above, I still don't understand why choroba's first suggestion to output a comma before every 'sentence' except the first won't work. Figuring out which is the first sentence is a lot simpler than figuring out which is the last.

Re^5: Size of a HASH (with more keys)
by LanX (Saint) on Jul 02, 2014 at 13:08 UTC
    The best solution would be using a JSON module. :)

    The second best is to push @lines, "JSON ENTRY" within the loop, and then to print join ",\n", @lines after the loop.

    See join for details.

    HTH! :)

    Cheers Rolf

    (addicted to the Perl Programming Language)

      I am trying to find a step by step guide on how to do the JSON encode using CPAN's module. In their library I know it's descriptive.

      But I as I read it quickly, I think they generate only JSON (which is a bit different than JSONP).

      Nevertheless, so If I have a HASH where all the info is stored that I want. How should I create a JSONP with this Module?

      $json_text   = $json->encode( $perl_scalar );

      This is the syntax from CPAN. Is the $perl_scalar my $testHASH in our case?

        > (which is a bit different than JSONP).

        try reading JSONP and explain the difference. You might figure out how to use JSON then! (hint: + padding)

        > Is the $perl_scalar my $testHASH in our case?

        from JSON

        $json_text = to_json($perl_scalar) Converts the given Perl data structure to a json string.

        If you try to understand JSON and Perl Data Structures you will see that they have the exactly same structure with little syntactic differences (enough to justify a module in that case)

        I.o.w. $perl_scalar is a ref to a HoH or HoA or AoH (... and so on) reflecting the JSON structure you desire.

        Please try ...

        • to read the links I gave you,
        • use search engines for example code
        • generally try experimenting with code¹
        ... to improve your coding skills.

        Hope this helped! :)

        Cheers Rolf

        (addicted to the Perl Programming Language)

        update

        ¹) use Data::Dumper or Data::Dump to inspect data structures

Re^5: Size of a HASH (with more keys)
by dorko (Prior) on Jul 02, 2014 at 16:38 UTC
    Hello,

    You might want to chop the $line variable before you print it.

    Good Luck,
    Brent

    -- Yup, I'm a Delt.