Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

L~R's solution has problems:

$data_array_ref = [ mykey1 => {firstkey => 'firstvalue', secondkey => 'secondval' }, mykey2 => {ninza => 'turtle', 'Hurricane' => 'Dennis'}, mykey3 => [ ['one', 'two', 'three'] ], mykey4 => [ [4, 5, 'three'], [6, 7, 'four'], [8, 9, 'five'] ], ]; for ( @$data_array_ref ) { print "$_ =>"; if ( ref $_ eq 'ARRAY' ) { print join ", ", @$_; } else { print join ", ", sort values %$_; } print "\n"; } __END__
output ====== mykey1 => HASH(0x1abefc0) =>firstvalue, secondval mykey2 => HASH(0x1abf0a4) =>Dennis, turtle mykey3 => ARRAY(0x1ab5148) =>ARRAY(0x1ab50d0) mykey4 => ARRAY(0x1ab52f8) =>ARRAY(0x1ab5190), ARRAY(0x1ab5208), ARRAY(0x1ab5280 +)

My solution:

$data_array_ref = [ mykey1 => {firstkey => 'firstvalue', secondkey => 'secondval' }, mykey2 => {ninza => 'turtle', 'Hurricane' => 'Dennis'}, mykey3 => [ ['one', 'two', 'three'] ], mykey4 => [ [4, 5, 'three'], [6, 7, 'four'], [8, 9, 'five'] ], ]; my $i = 0; while ($i < @$data_array_ref) { my $key = $data_array_ref->[$i++]; my $val = $data_array_ref->[$i++]; print "$key => "; if (ref($val) eq 'HASH') { print join ', ', values %$val; } else { print join ', ', map { @$_ } @$val } print "\n"; } __END__ output ====== mykey1 => firstvalue, secondval mykey2 => turtle, Dennis mykey3 => one, two, three mykey4 => 4, 5, three, 6, 7, four, 8, 9, five

It be somewhat easier if your topmost data structure was a hash, not an array.

map { @$_ } @$_ flattens a ref to an array of arrays of scalars, as opposed to @$_ which only flattens a ref to an array of scalars.


In reply to Re^2: Parsing complex data by ikegami
in thread Parsing complex data by chanakya

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, 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?
    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 wandering the Monastery: (3)
    As of 2020-10-31 13:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (289 votes). Check out past polls.

      Notices?