Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a data structure like the one given below(sample). This one is sample only. This is build dymanically based on an array. The no of elt in the array may vary.
So the DS may be a HoHoA or HoHoH...oA.
,
I want to access all the keys and array from similar structure.
,
I am using Data::Diver to create this structure
$summaryhash = 'AMER'; $VAR2 = { 'N1' => [ [ [ [ '1' ], '9.4495' ], '97.1521' ], '22.0331' ], 'Security' => [ [ [ [ [ '1' ], '98.6315' ], '55.2082' ], '40.8162' ], '47.5309' ], }
i want something like this key1,key2..... value(array)
Thanks in Adv

Replies are listed 'Best First'.
Re: Accessing the Array values in a multilevel hash
by BrowserUk (Patriarch) on Dec 13, 2006 at 11:51 UTC
    i want something like this key1,key2..... value(array)

    You're gonna have to clarify this question before you get a response.

    For example, what is your question? You say you want something like

    key1,key2..... value(array)

    but what does that mean?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Accessing the Array values in a multilevel hash
by jonadab (Parson) on Dec 13, 2006 at 13:51 UTC

    To the extent that your sample data structure reflects the structure of your actual data structure, what you have here is not actually an HoAoA...oA as such, because at various levels you have a heterogenous array of both arrays and strings (and the strings happen to look an awful lot like numbers). That's... *weird*. It's possible to traverse it, but at every step you'll have to check whether the next element is an array reference, a string, or what...

    sub flatten { # Depth-first traversal. my @scalar; foreach my $item (@_) { if (ref $item eq 'ARRAY') { push @scalar, flatten(@$item); } elsif (ref $item eq 'HASH') { push @scalar, $_, flatten($$item{$_}) for keys %$item; } elsif (ref $item) { push @scalar, ref $item; } else { push @scalar, $item; } } return @scalar; } print join ', ', flatten $VAR2; print "\n";

    But perhaps the more important question is, why is your data structure like this in the first place? All I've done is traverse your structure and extract all the non-structure bits, but if the structure has meaning, I'm not seeing it from the sample data. Yet, presumably there was some reason for all that structure. Maybe we could see it ourselves if you posted (a few of) the actual data. Or maybe not, but it might be worth a try. Perhaps if you describe for us what the structure means, we could understand better what you want to do with it.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.
Re: Accessing the Array values in a multilevel hash
by f00li5h (Chaplain) on Dec 13, 2006 at 11:55 UTC

    I'm a little confused as to the order you want to traverse this structure in?

    I also have no idea what a structure like that could be used for, but that's not really the point.

    when you say

    key1,key2..... value(array)
    , which values from your data do they represent?
    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;