http://qs1969.pair.com?node_id=475245


in reply to Parsing complex data

chanakya,
Since you have omitted details of your requirements I am going to guess that you know you have an AoHo(H or A) and that you want the keys of the second level H and corresponding values of the (H or A) regardless of which it is.
for ( @$data } { print "$_ =>"; if ( ref $_ eq 'ARRAY' ) { print join ", ", @$_; } else { print join ", ", sort values %$_; } print "\n"; }
Note: You may have to modify this a bit to get a specific order with your hashes if the simple sort I provided isn't satisfactory. You will also have to modify the depth of ref depending on how many levels deep you need to go. IOW - this isn't fully working code but an idea of how to get started.

Cheers - L~R

Update:Data::Dump::Streamer and/or Data::Dumper may also be of some help. As socketdave points out, your data structure is missing commas and unbalanced and it is hard to say for sure what you want without some more specifics

Replies are listed 'Best First'.
Re^2: Parsing complex data
by socketdave (Curate) on Jul 15, 2005 at 15:19 UTC
    The structure of your input data also seems pretty hosed to me, but I could be missing something... shouldn't it be something closer to:
    my $data = [ mykey1 => { 'firstkey' => 'firstvalue', 'secondkey' => 'secondval' + }, mykey2 => { 'ninza' => 'turtle', 'Hurricane' => 'Dennis' }, + mykey3 => [ [ 'one', 'two', 'three' ] ], mykey4 => [ [ 4, 5, 'three' ], [ 6, 7, 'four' ], [ 8, 9, 'five' ] +], ];
Re^2: Parsing complex data
by ikegami (Patriarch) on Jul 15, 2005 at 15:38 UTC

    L~R's solution has problems:

    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.

      ikegami,
      Limbic's solution has problems

      It certainly does. This is one of those posts where my brain auto-corrected missing and incorrect information. After the initial post I realized it was lacking and noted it. Thanks for spending more than 2 seconds reading the question before replying - it is afterall Friday.

      Cheers - L~R