Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

accessing data structure

by CColin (Scribe)
on Nov 16, 2007 at 12:49 UTC ( #651182=perlquestion: print w/ replies, xml ) Need Help??
CColin has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on accessing data structure
Select or Download Code
Re: accessing data structure
by lupey (Monk) on Nov 16, 2007 at 13:35 UTC
    Seems you are new to references. Your question will be answered by reading the following Perl documentation:

    perlreftut and perldsc

    lupey

Re: accessing data structure
by agianni (Hermit) on Nov 16, 2007 at 13:39 UTC
    Your main problem is that @{results} is not the same as @{$results}, which is what I think you're looking for. Always use strict and use warnings in your code and you'll get a warning (actually a fatal error) when you try to run code like this.
    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
      Thanks - staring at the code too long I missed it. I had warnings on but didn't get what I'd done wrong. Now I have a real array structure I can start to unravel. Colin
        I had warnings on but didn't get what I'd done wrong.
        Perhaps you already tried this, but sometimes use diagnostics; will provide a little more detailed help in figuring out what the warnings really mean.
Re: accessing data structure
by jbert (Priest) on Nov 16, 2007 at 13:42 UTC
    As well as reading up on references, please make your code run under use strict; and use warnings;.

    This would have been useful in this case because you would have got the warning:

    Global symbol "@results" requires explicit package name at - line 2.
    which would have given you a hint that you had messed up, writing @{results} instead of @{$results} (or just @$results).

    strict and warnings are really useful and save a lot of time for you and for people answering questions, too.

      Hi, I had read up on references and also had strict and warnings on. I don't take posting to this august list so lightly, but references aren't so easy in Perl, at any rate that's the way it is for me. Never attribute to indolence what can first be explained by ignorance... Thanks anyway to the person who spotted the issue.
        Sorry if I seemed rude, I didn't mean to cause any offence.

        My reference to references (ha!) was in the context of the previous reply. I hope that helps change the tone a little (without that, it does seems ruder to me).

        I don't understand how you got the results you did with strict on, this code fails to execute for me (compilation error):

        #!/usr/bin/perl use strict; my @dereferenced_array = @{results}; print "not reached\n"; # doesn't execute
        perl 5.8.8, linux.
Re: accessing data structure
by glide (Pilgrim) on Nov 16, 2007 at 16:24 UTC
    to walk inside the hash, use the keys function. For example:
    for my $key (keys(%{$results})) { print Dumper $result->{$key}; }
Re: accessing data structure
by planetscape (Canon) on Nov 17, 2007 at 10:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://651182]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-08-22 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (146 votes), past polls