Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Yes. In the spirit of TMTOWTDI, there are several ways to represent your data structure. Which you choose is largely up to you. The information provided below should serve as an overview only; always consult a module's complete documentation for usage, bugs, and caveats.

  1. Dumpvalue

More of a prettyprinter than a serializer, this method provides the view of your data structure that is most like the output from Perl's debugger. The output format may be modified by setting numerous options. Dumpvalue can even dump the symbol tables of whole packages.

use strict; use warnings; use Dumpvalue; my $ref = { a => [ 1, 2, 3 ], b => [ { X => 1, Y=> 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; print "Dumpvalue:\n"; Dumpvalue->new->dumpValue( $ref );

Output:

Dumpvalue: 'a' => ARRAY(0x460ee8) 0 1 1 2 2 3 'b' => ARRAY(0x4e3e50) 0 HASH(0x4d964c) 'X' => 1 'Y' => 2 1 HASH(0x4e3e14) 'X' => ARRAY(0x4d967c) 0 1 1 2 2 3 'Y' => ARRAY(0x4e3d9c) 0 4 1 5 2 6 'Z' => ARRAY(0x4e3dd8) 0 7 1 8 2 9
  1. Data::Dumper

Possibly the most popular choice, Data::Dumper outputs actual Perl code. As such, it is both a prettyprinter and a serializer. Data::Dumper can handle self-referential data structures, and evaling Data::Dumper's output reconstitutes an exact copy of the original data structure. This module is useful both for saving data structures to disk and DBM files and for passing them to other processes. As with Dumpvalue, Data::Dumper's output is configurable.

use strict; use warnings; use Data::Dumper; my $ref = { a => [ 1, 2, 3 ], b => [ { X => 1, Y=> 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; print "Data::Dumper:\n"; print Dumper( $ref );

Output:

Data::Dumper: $VAR1 = { 'a' => [ 1, 2, 3 ], 'b' => [ { 'X' => 1, 'Y' => 2 }, { 'Z' => [ 7, 8, 9 ], 'X' => [ 1, 2, 3 ], 'Y' => [ 4, 5, 6 ] } ] };
  1. YAML's Dump

Another prettyprinter/serializer combo. YAML's output format is also controllable and hash keys are sorted by default. Since YAML does not eval to de-serialize, it is safer than Data::Dumper when you're not sure who else might have access to your files. Data structures serialized from Perl using YAML can be accurately read and processed using YAML in Java, Javascript, PHP, Python, Ruby, and Tcl (and of course, Perl). Likewise, data structures may be serialized in any of these languages and be usable by Perl.

use strict; use warnings; use YAML; my $ref = { a => [ 1, 2, 3 ], b => [ { X => 1, Y=> 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; print "YAML::Dump:\n"; print Dump( $ref );

Output:

YAML::Dump: --- a: - 1 - 2 - 3 b: - X: 1 Y: 2 - X: - 1 - 2 - 3 Y: - 4 - 5 - 6 Z: - 7 - 8 - 9
  1. Data::Dump::Streamer, by demerphq

If it's by our very own demerphq, it must be good.

Data::Dump::Streamer offers more accurate and more readable output, and uses less memory, than does Data::Dumper. Hash keys are sorted and the sort order is configurable. Data::Dump::Streamer's output, like that of Data::Dumper, may be evaled back to its original form.

use strict; use warnings; use Data::Dump::Streamer; my $ref = { a => [ 1, 2, 3 ], b => [ { X => 1, Y=> 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; print "Data::Dump::Streamer:\n"; print Dump( $ref );

Output:

Data::Dump::Streamer: $HASH1 = { a => [ 1, 2, 3 ], b => [ { X => 1, Y => 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] } ] };
See also: Test::Struct
  1. GraphViz::Data::Structure is one of two modules I know of that literally draw you a picture of your data structure. Great for the visually-oriented, but none of the de-serializing or multi-lingual features of the above alternatives.
use strict; use warnings; use GraphViz::Data::Structure; my $data_structure = { a => [ 1, 2, 3 ], b => [ { X => 1, Y => 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; my $gvds = GraphViz::Data::Structure->new( $data_structure, Orientation => 'ver +tical' ); print $gvds->graph()->as_png("gvds.png");

Output

  1. GraphViz::Data::Grapher is the other of the two, and happens to be my personal favorite. No reconstituting original structures or sharing them with other languages such as Python or Ruby; but this does create very nice illustrations of complex data structures for documentation or perhaps teaching purposes.
use strict; use warnings; use GraphViz::Data::Grapher; my $structure = { a => [ 1, 2, 3 ], b => [ { X => 1, Y => 2 }, { X => [ 1, 2, 3 ], Y => [ 4, 5, 6 ], Z => [ 7, 8, 9 ] }, ], }; my $graph = GraphViz::Data::Grapher->new($structure); print $graph->as_png("gvdg.png");

Output

 

For more on visualizing hash structure, please see: Not Exactly a Hash Tutorial

 

References (incomplete and in no particular order):


In reply to Answer: How can I visualize my complex data structure? by QandAEditors

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (6)
    As of 2014-12-28 14:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (181 votes), past polls