Given the following table from a database:

+------+-----------+--------------------+ | id | title | comments | +------+-----------+--------------------+ | 2911 | Rush Hour | Fun :-) | +------+-----------+--------------------+ | 3217 | Titanic | Drama, too long | +------+-----------+--------------------+ | 6518 | Bambi | | +------+-----------+--------------------+ | 7388 | Star Wars | "I'm your father!" | +------+-----------+--------------------+

In a JSON structure, this would be something like the following (without newlines to show the records):

[{"id":2911,"title":"Rush Hour","comments":"Fun :-)"} ,{"id":3217,"title":"Titanic","comments":"Drama, too long"} ,{"id":6518,"title":"Bambi"} ,{"id":7388,"title":"Star Wars","comments":"\"I'm your father!\""} ]

My intention was to read this "special" file and translate it into a tab delimited CSV-like file, filling missing fields with default values.

The problem parsing this very long line is that it's not possible to split by comma without looking at the context, because it appears in data, just like colons, quotes (escaped with a backslash), and structure indicators ([ ] and { }, not the regex special chars, BTW).

JSON module can parse that file without problems, returning the AoH structure just like it was intended:

use JSON; use Data::Dumper; my $json = <DATA>; my $data = decode_json($json); print Dumper $data; __DATA__ [{"id":2911,"title":"Rush Hour","comments":"Fun :-)"},{"id":3217,"titl +e":"Titanic","comments":"Drama, too long"},{"id":6518,"title":"Bambi" +},{"id":7388,"title":"Star Wars","comments":"\"I'm your father!\""}]
$VAR1 = [ { 'title' => 'Rush Hour', 'id' => 2911, 'comments' => 'Fun :-)' }, { 'title' => 'Titanic', 'id' => 3217, 'comments' => 'Drama, too long' }, { 'title' => 'Bambi', 'id' => 6518 }, { 'title' => 'Star Wars', 'id' => 7388, 'comments' => '"I\'m your father!"' } ];

Of course, if I had to manage this structure in memory, I'd use HoH (with id as the main key) or HoA (to save space if all other fields are present in every record) instead of AoH... Or maybe a single hash (as you proposed first) with a key composed of the id value of a record and the field name for every other values... But this should be another discussion.

Was I clear this time? I'm sorry, I'm translating from Spanish on the fly... ;-)


In reply to Re^2: Move data into AoH by vitoco
in thread Move data into AoH by vitoco

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.