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

I apologize for lack of information / explanations.

What I'm doing:

I am creating a Hash that will store the certain data from a database. And then I need to use that Hash's data to create a JSONP file format.

JSONP format requires certain structure: http://en.wikipedia.org/wiki/JSONP

That is why I need to remove the comma at the end of the last sentence, otherwise my .HTML file wont recognize it as valid JSONP file.

Hope it is more clear now.

BR, David

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

      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)

        Hello,

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

        Good Luck,
        Brent

        -- Yup, I'm a Delt.