Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Move data into AoH

by vitoco (Friar)
on Sep 26, 2013 at 13:39 UTC ( #1055832=note: print w/ replies, xml ) Need Help??


in reply to Re: Move data into AoH
in thread Move data into AoH

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


Comment on Re^2: Move data into AoH
Select or Download Code
Re^3: Move data into AoH
by ww (Bishop) on Sep 26, 2013 at 19:13 UTC
    Was I clear this time?

    Yes! Thank you for making the effort, and for posting your code which will likely be a blesssing for future Seekers....

    I'm translating from Spanish on the fly... ;-)

    ... and in this case, doing it so well that I think I'm entirely clear about your intent and suspect you know a good deal more about JSON, likely know more about Perl and certainly have had an ups-and-downs intro to the Monastery.

    So go forth; sin no more, but confess (with code and data) here if you run into further problems.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Well, I'm used to mangle tons of data from many different sources and formats, but I didn't know about JSON. I only figured out about the data format and potential problems. That's why I started this thread! As some monks pointed out, JSON module was what I needed this time... at least for a reasonable number of records! :-)

      BTW: in Re^2: Move data into AoH I said: "if I had to manage this structure in memory". I meant: "if I had to manage this dataset in memory".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055832]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-12-28 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls