in reply to Accessing the Array values in a multilevel hash
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.
|
|---|