Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

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

So what do you want to do with it? I'd be inclined to turn it into an object then write a few accessor methods to pull out interesting fragments. Consider:

use strict; use warnings; my %hash = ( 'some item' => { 'source' => 'a source', 'conversions' => { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2', 'conversion 3' => 'value 3', 'conversion 4' => 'value 4', 'conversion 5' => 'value 5', }, 'whatever' => 'eh', }, 'another item' => { 'source' => 'another source', 'conversions' => { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2', }, 'whatever' => 'eh', }, 'one more item' => { 'source' => 'another source', 'whatever' => 'eh', }, ); my $obj = bless {data => \%hash}; for my $item ($obj->getItems ()) { print "$item->{name}: $item->{source}, $item->{whatever}\n"; } my %conversions = $obj->getConversions ('some item'); print "$_ => $conversions{$_}\n" for sort keys %conversions; sub getItems { my ($self) = @_; my $data = $self->{data}; return map {{name => $_, %{$data->{$_}}}} keys %$data; } sub getItem { my ($self, $itemName) = @_; return if ! exists $self->{data}{$itemName}; return $self->{data}{$itemName}; } sub getConversions { my ($self, $itemName) = @_; return if ! exists $self->{data}{$itemName}; my $item = $self->{data}{$itemName}; return if ! exists $item->{conversions}; return %{$item->{conversions}}; }


some item: a source, eh another item: another source, eh one more item: another source, eh conversion 1 => value 1 conversion 2 => value 2 conversion 3 => value 3 conversion 4 => value 4 conversion 5 => value 5

Such an approach is worth doing if your data structure has a consistent structure and you need to perform a bunch of queries against it. If the data is not consistently structured then it's going to be endless hard work no matter what because you simply have to deal with special cases and that is always a source of pain.

Note that I fixed a bunch of syntax errors in the structure you gave so I presume that your sample doesn't map on to anything real in any sense.

True laziness is hard work

In reply to Re^5: Nested Data Structures for Dummies? by GrandFather
in thread Nested Data Structures for Dummies? by jedikaiti

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2018-02-25 21:52 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (315 votes). Check out past polls.