Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

traversing a list of objects

by patrickrock (Beadle)
on Dec 07, 2004 at 20:32 UTC ( #412997=perlquestion: print w/replies, xml ) Need Help??

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

when using Net::Amazon::Request::Wishlist the docs state:

"Upon success, the response's properties() method will return a list of Net::Amazon::Property::* objects" So the following code:
use Net::Amazon; use Net::Amazon::Request::Wishlist; use Data::Dumper; $token = 'token'; $wishid = 'wishid'; my $ua = Net::Amazon->new(token => $token); my $req = Net::Amazon::Request::Wishlist->new(wishlist => $wishid); # Response is of type Net::Amazon::Response::Wishlist my $resp = $ua->request($req); printer Dumper($resp);
returns this (from the dumper function):
$VAR1 = bless( { 'xmlref' => { 'Details' => [ { 'ReleaseDate' => '01 F +ebruary, 2003', 'Upc' => '636920004479 +', 'NumMedia' => '1',
etc...

What I am unsure about is the syntax I need to traverse this. I know that I am looking at some kind of hash of hashes, but I'm just kind of lost. I feel like I should be able to do something like:

print $resp{xmlref}{Details}{ReleaseDate};

But that doesn't seem to be working. I think I just don't understand the syntax of what to do. Can you guys help?

Replies are listed 'Best First'.
Re: traversing a list of objects
by simonm (Vicar) on Dec 07, 2004 at 21:11 UTC
    Perhaps you want $resp->{xmlref}{Details}[0]{ReleaseDate}? The arrow means that you want to follow a reference, and the square brackets find array elements.

    One way you can explore this kind of structure is by using Data::Dumper to show the results of a partial access:

    print Dumper( $resp ); print Dumper( $resp->{xmlref} ); print Dumper( $resp->{xmlref}->{Details} );

    Check out the perlref manpage for more information on the reference syntax.

Re: traversing a list of objects
by ikegami (Patriarch) on Dec 07, 2004 at 21:27 UTC

    $resp is a reference to a hash, not a hash, so
    print $resp->{...
    or
    print $$resp{...

    Also, You seem to not have noticed the [, so
    print $resp->{xmlref}{Details}[$i]{ReleaseDate}
    or
    print $$resp{xmlref}{Details}[$i]{ReleaseDate}

    To print release date of all items:

    print($_->{ReleaseDate}, $/) foreach (@{$$resp{xmlref}{Details}});

    To store the release dates of all items into an array:

    @release_dates = map { $_->{ReleaseDate} } @{$$resp{xmlref}{Details}};
      print $$resp{...

      I really really dislike that syntax. In large part because it's sooo Perl4.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: traversing a list of objects
by BrowserUk (Patriarch) on Dec 07, 2004 at 21:13 UTC

    Try

    print $resp->{xmlref}{Details}{ReleaseDate};

    And then read references quick reference (Re: push)


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: traversing a list of objects
by IOrdy (Friar) on Dec 08, 2004 at 13:20 UTC
    I might be missing the point but why are you poking around the internals of the object?

    The docs for Net::Amazon::Response shows that properties() returns an array of objects (each of which is a Net::Amazon::Property::* object I guess)
    if($resp->is_success) { foreach my $property ($resp->properties) { # $property isa Net::Amazon::Property object # print $property->as_string, $/; print $property->ProductName, ' ', $property->ReleaseDate, $/; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2023-09-30 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?