Hi, I have some XML represented in a variable $results which returned a structure using Data::Dumper as follows:
print Dumper ($results); $VAR1 = [ { 'somevar1' => { 'othervar1' => 'somevalue' 'othervar2' => 'somevalue' } 'somevar2' => { 'othervar1' => 'somevalue' 'othervar2' => 'somevalue' } }, { 'somevar1' => { 'othervar1' => 'somevalue' 'othervar2' => 'somevalue' } 'somevar2' => { 'othervar1' => 'somevalue' 'othervar2' => 'somevalue' } } ];
And so on for many more iterations... So, what I think is that I have an anonymous array of a hash of hashes? If the first value is an array, I'd like to do a foreach loop over it to get at the other values. Try as I might I'm not succeeding in unravelling the $results array in perl code. I can access individual elements as follows:
$results->[0]{'somevar1'}{'othervar1'}
But - this does not solve the problem in that it is necessary to know how many elements of the array there are in order to access all such values. Attempting to dereference the array with
@dereferenced_array = @{results}; $no_elements_in_array = @dereferenced_array;
Always produces a result of 1 for $no_elements_in_array even when there are lots of elements (basically does not work as expected). Clearly I am missing something in working with this data structure. My first question is how do I get at the number of elements (ie. first level hashes) in the array? Then at least I can write a for loop as follows and solve the immediate problem:
for ($i = 0; $i < $no_elements_in_array; i++) { $value = $results->[i]{'somevar1'}{'othervar1'}; print "$value\n"; }
Second, why does my attempt to get at the array fail? Thanks Colin

In reply to accessing data structure by CColin

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, details, 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, summary, 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.