Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Data from a Hash/Array

by foggy3657 (Novice)
on Sep 05, 2012 at 19:36 UTC ( #991924=perlquestion: print w/ replies, xml ) Need Help??
foggy3657 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I should tell you I'm a real newbie. Only just half way through my first book. But I figured I would try a project to get me going - doing rather than just reading. I am using an API to get data returned.

#!/usr/bin/perl use warnings; use strict; use WebService::Tesco::API; # Code to log in removed my $code = $tesco->product_search({ searchtext => $pn, extendedinfo => + 'N' });
This returns the following
{ "StatusCode": 0, "StatusInfo": "Command Processed OK", "PageNumber": 0, "TotalPageCount": 1, "TotalProductCount": 1, "PageProductCount": 1, "Products": [ { "BaseProductId": "50043662", "EANBarcode": "5000175411118", "CheaperAlternativeProductId": "", "HealthierAlternativeProductId": "", "ImagePath": "http://img.tesco.com/Groceries/pi/118/5000175411118/IDSh +ot_90x90.jpg", "MaximumPurchaseQuantity": 99, "Name": "Oxo 12 Chicken Stock Cubes 71G", "OfferPromotion": "Price Drop Was 1.13 Now 1.00 ", "OfferValidity": "valid from 30/7/2012 until 9/9/2012", "OfferLabelImagePath": "http://www.tesco.com/Groceries/UIAssets/I/Site +s/Retail/Superstore/Online/Product/pos/save.png", "Price": 1, "PriceDescription": "1.41 each", "ProductId": "254881114", "ProductType": "QuantityOnlyProduct", "UnitPrice": 1.41, "UnitType": "100g" } ] }

I am looking to create a variable that holds the ProductId "254881114" As I need this number to make my next URL. Any help would be gratefully received. Best wishes. James.

Comment on Data from a Hash/Array
Select or Download Code
Re: Data from a Hash/Array
by toolic (Chancellor) on Sep 05, 2012 at 19:44 UTC
    Assuming those colons are really fat commas (=>):
    use warnings; use strict; my $code = { "StatusCode"=> 0, "StatusInfo"=> "Command Processed OK", "PageNumber"=> 0, "TotalPageCount"=> 1, "TotalProductCount"=> 1, "PageProductCount"=> 1, "Products"=> [ { "BaseProductId"=> "50043662", "EANBarcode"=> "5000175411118", "CheaperAlternativeProductId"=> "", "HealthierAlternativeProductId"=> "", "ImagePath"=> "http=>//img.tesco.com/Groceries/pi/118/5000175411118/ID +Shot_90x90.jpg", "MaximumPurchaseQuantity"=> 99, "Name"=> "Oxo 12 Chicken Stock Cubes 71G", "OfferPromotion"=> "Price Drop Was 1.13 Now 1.00 ", "OfferValidity"=> "valid from 30/7/2012 until 9/9/2012", "OfferLabelImagePath"=> "http=>//www.tesco.com/Groceries/UIAssets/I/Si +tes/Retail/Superstore/Online/Product/pos/save.png", "Price"=> 1, "PriceDescription"=> "1.41 each", "ProductId"=> "254881114", "ProductType"=> "QuantityOnlyProduct", "UnitPrice"=> 1.41, "UnitType"=> "100g" } ] } ; print $code->{Products}[0]{ProductId},"\n"; __END__ 254881114

    See also:

      Thank you very much toolic. Your assistance was really appreciated. I will read up on the links, but an initial glance tells me I've a long way to go before I understand half of it. Couple of questions. Not sure I understood your reference to fat commas - they were in fact colons - but your code worked in any event. What was the returned data, was it a hash or an array? I need to read something a little easier than perldsc to understand the code you posted. Once again thank you for helping Best wishes. James.

        Thank you very much toolic.
        You're welcome.
        Not sure I understood your reference to fat commas
        See Fat_comma and Comma Operator

        Regarding my solution... don't use it :) Other monks are on to the real problem, that you need to look into parsing JSON.

Re: Data from a Hash/Array
by NetWallah (Abbot) on Sep 05, 2012 at 19:56 UTC
    It looks like the data returned is in JSON/YAML format. Please review documentation for the Tesco::API to find out the return type.

    Please search cpan.org for a module suitable to interpret the the return type.
    That will allow you to reference the variable desired easily.

    If this is a test situation, you can use a regex like:

    my ($prodid) = $code=~/"ProductId": "([^"]+)"/;

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      It looks like the data returned is in JSON/YAML format.
      I think you're onto something. The WebService::Tesco::API POD does not say, but looking through the source code, product_search eventually calls get...
      sub get { ... return JSON->new->utf8->decode($res);
Re: Data from a Hash/Array
by rpnoble419 (Pilgrim) on Sep 05, 2012 at 19:56 UTC
    James;

    Welcome to the world of Perl.

    It looks like your data is in JSON format. Is this correct? If so, you should look at the JSON module on CPAN.org. Go to the section called HOW DO I DECODE A DATA FROM OUTER AND ENCODE TO OUTER.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-09-16 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (32 votes), past polls