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

> Hope it is more clear now.

nope

But ...

> I am creating a Hash that will store the certain data from a database

... and ...

> ... remove the comma at the end of the last sentence

don't fit together, because hashes have no order.

see also How (Not) To Ask A Question and How do I post a question effectively?

Cheers Rolf

(addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^4: Size of a HASH (with more keys)
by David92 (Sexton) on Jul 02, 2014 at 12:59 UTC
    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.

      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.

      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?

      Hello,

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

      Good Luck,
      Brent

      -- Yup, I'm a Delt.