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

How do I iterate through this data set?

by uG (Scribe)
on May 21, 2011 at 19:11 UTC ( #906084=perlquestion: print w/ replies, xml ) Need Help??
uG has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to iterate through what I thought was an array of lists. Here is the first piece of the dataset:

$VAR1 = { 'timestamp' => '2011-05-21T18:21:40.588Z', 'version' => '1.10.0', 'ack' => 'Success', 'paginationOutput' => { 'totalPages' => '59', 'pageNumber' => '1', 'entriesPerPage' => '100', 'totalEntries' => '5812' }, 'searchResult' => { 'count' => '100', 'item' => [ { 'primaryCategory' => { 'category +Id' => '63850', 'category +Name' => 'Mixed Items & Lots' }, 'distance' => { 'unit' => 'mi', 'content' => '18 +75.0' }, 'sellingStatus' => { 'convertedC +urrentPrice' => { + 'currencyId' => 'USD', + 'content' => '1.0' + }, 'currentPri +ce' => { + 'currencyId' => 'USD', + 'content' => '1.0' + }, 'sellingSta +te' => 'Active', 'timeLeft' +=> 'P3DT18H28M2S', 'bidCount' +=> '0' },

You can also view the full thing at ugexe.com/file.txt but it repeats itself a lot (and i've included the relevant part). Here is how I am attempting to iterate and grab the values:

foreach my $n ( $hash->{'searchResult'}->{'item'} ) { my $item = { item_link => $n->{'viewItemURL'}, item_number => $n->{'itemId'}, title => $n->{'title'}, subtitle => $n->{'subtitle'}, current_bid => $n->{'currentPrice'}->{'content'}, }; }

My error comes when I try to create the anonymous list using $n->{'viewItemURL'} (or whatever viewItemUrl may be). It tells me $n is not a hash reference. So I am a little confused on how I am suppose to get the values from this dataset.

Comment on How do I iterate through this data set?
Select or Download Code
Re: How do I iterate through this data set?
by Corion (Pope) on May 21, 2011 at 19:18 UTC
    print ref $hash->{'searchResult'}->{'item'};

    will tell you that what you have is an array reference, not a hash.

    To get at the array elements for the loop, use

    foreach my $n (@{ $hash->{'searchResult'}->{'item'} }) { ... }

    Also see References Quick Reference.

Re: How do I iterate through this data set?
by 7stud (Deacon) on May 21, 2011 at 20:28 UTC

    You have this hash:

    { item => [ ... ... }

    Immediately following the arrow is a bracket, and a bracket is the syntax for an array reference. So when you use the 'item' key to get its corresponding value, you get an array reference back. You are are treating the array reference as a hash reference.

    Looking at the original data, the array has a bunch of hash references for elements. Hash references are denoted by braces: {...}

    I would rename the variable in Corion's loop to help you keep track of what type you are getting out of the array:

    for my $href (@{ $hash->{'searchResult'}->{'item'} }) { ... }
Re: How do I iterate through this data set?
by planetscape (Canon) on May 22, 2011 at 13:58 UTC

    The answers to How can I visualize my complex data structure? can help you identify what sort of beast you actually have. I often can't parse lots of square/curly brackets and what not due to my dyslexia, and pictures may help.

    HTH,

    planetscape
Re: How do I iterate through this data set?
by sundialsvc4 (Monsignor) on May 23, 2011 at 12:19 UTC

    There are several varieties of “data walker” objects available in CPAN, which know how to crawl through an arbitrary data structure in a depth-first or breadth-first fashion.   They maintain an internal state of where they are, and return “the next” item whenever an appropriate method is called.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-07-23 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (139 votes), past polls