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

This thing about the comma bothers me, maybe I don't get what your're doing or why you require a comma before you delete it (just sounds wrong and brittle). Should you be using a join? This'll make sure that your separator is not added at the end of the last item in the list.

Replies are listed 'Best First'.
Re^2: Size of a HASH (with more keys)
by David92 (Sexton) on Jul 02, 2014 at 12:20 UTC

    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
      > 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.