Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Hash of Hashes

by ewhitt (Scribe)
on Mar 09, 2008 at 09:44 UTC ( [id://673078]=perlquestion: print w/replies, xml ) Need Help??

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

I have been trying to figure out hash of hashes. I have found a few URLs, but the examples aren't clicking. Here is the code I am playing with and output.
use Net::Amazon; my $ua = Net::Amazon->new(token => 'xxxxxxxxxxxxxxxxxxxx', locale => ' +us'); my $response = $ua->search(browsenode=>"3839", mode=>"books", keywords +=>"perl"); if ($response->is_success()) { for my $prop ($response->properties) { $books{$prop->publication_date()}{'title'} = $prop->t +itle(); $books{$prop->publication_date()}{'authors'} = join(", ",$p +rop->authors()); $books{$prop->publication_date()}{'isbn'} = $prop->isbn( +); $books{$prop->publication_date()}{'pages'} = $prop->numpa +ges(); $books{$prop->publication_date()}{'publisher'} = $prop->publi +sher(); $books{$prop->publication_date()}{'image_small'} = $prop->Image +UrlSmall(); $books{$prop->publication_date()}{'image_medium'} = $prop->Image +UrlMedium(); } print Dumper %books; }
Data Dumper outputs
$VAR1 = '2005-07-12'; $VAR2 = { 'image_medium' => 'http://ecx.images-amazon.com/images/I/21m +kODqYmdL.jpg', 'authors' => 'Damian Conway', 'title' => 'Perl Best Practices', 'isbn' => '0596001738', 'publisher' => 'O\'Reilly Media, Inc.', 'image_small' => 'http://ecx.images-amazon.com/images/I/11Ol +nA6slCL.jpg', 'pages' => '542' };
I expected $VAR2's content to be under $VAR1? On a side note, does anyone know why there is a "\" in "O'Reilly" in the publisher key?

Replies are listed 'Best First'.
Re: Hash of Hashes
by FunkyMonk (Chancellor) on Mar 09, 2008 at 09:52 UTC
    $VAR2 is "under" $VAR1, it's your Data::Dumper call that's wrong. You need to backslash the hash (to create a hashref):
    print Dumper \%books

    Apostrophes need to be quoted inside a single-quoted string, otherwise it will be interpreted as being the end of the string.

    Update: must use Spelling before writing "apostrophes"

Re: Hash of Hashes
by Corion (Patriarch) on Mar 09, 2008 at 09:53 UTC

    The Dumper routine of Data::Dumper takes a reference, so you'll see the "real" data structure by

    print Dumper \%books;

    The backslash in 'O\'Reilly Media, Inc.' is in the output to quote the single-quoted string.

      Wow, the whole time I thought it was the hash statements. Thank you all very much for the insight. In regards to the backslash, are there other options than single quotes? As you can see in my code, it is being returned from the Amazon query.

        There is another way for Data::Dumper to display the results and that is by simply turning on the Useqq flag like this $Data::Dumper::Useqq = 1;, though it is not recommended. This way you'll get double quotes instead of single quotes.

        As a side note, I don't know what you are trying to do with your code, but you may get into trouble if you try to get two books published on the same date. If that happens, from your code, the first book retrieved will get overwritten and you won't know it was ever there. My advice to overcome that situation is to make, for each date, an array of hash references with the other book properties.

        Something like

        if ($response->is_success()) { for my $prop ($response->properties) { my %info; $info{'title'} = $prop->title(); $info{'authors'} = join(", ",$prop->authors()); $info{'isbn'} = $prop->isbn(); $info{'pages'} = $prop->numpages(); $info{'publisher'} = $prop->publisher(); $info{'image_small'} = $prop->ImageUrlSmall(); $info{'image_medium'} = $prop->ImageUrlMedium(); push $books{$prop->publication_date()}, \%info; } print Dumper \%books; }
Re: Hash of Hashes
by bingos (Vicar) on Mar 09, 2008 at 09:54 UTC

    Try something like

    print Dumper \%books;

    Your hash was becoming a list.

    There is a '\' in front of "'", because Dumper is escaping that character.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found