Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Pulling a hash value in a data structure

by Freethinker0 (Initiate)
on Jan 25, 2011 at 16:42 UTC ( #884170=perlquestion: print w/ replies, xml ) Need Help??
Freethinker0 has asked for the wisdom of the Perl Monks concerning the following question:

Sorry if the question is too rookie, I'm not a great, nor experienced coder. I am using the VirusTotal API to pull some info about some files. It returns a hash, which is too complicated for my feeble brain. Data Dumper shows the following data structure, which is in $res1. To be clear, the ultimate goal is to be able to pull out 'Eicar-Test-Signature':
$VAR1 = { 'report' => [ '2011-01-25 10:30:50', { 'AntiVir' => 'Eicar-Test-Signature', ... } ] };
It looks to me like a hash within an array within a hash within a hash. If I do print ($res1->{'report'}[0]); it returns '2011-01-25 10:30:50'. So far so good. I thought from here I need to do is change the code to print ($res1->{'report'}[0]->{'AntiVir'}); to get the key value of 'Eicar-Test-Signature'. Instead it returns nothing. I know I am close, but after a few hours on this, I am turning to you guys. Thanks much.

Comment on Pulling a hash value in a data structure
Select or Download Code
Replies are listed 'Best First'.
Re: Pulling a hash value in a data structure
by wfsp (Abbot) on Jan 25, 2011 at 16:48 UTC
    Nearly. Try 1 instead of 0. The hash is the second element of the array.
Re: Pulling a hash value in a data structure
by toolic (Bishop) on Jan 25, 2011 at 18:27 UTC
    Instead it returns nothing.
    If you use strict and warnings you will get something -- an error message.

    wfsp is correct. Dumpvalue may also be useful when you're trying to remember how to dereference because it shows the numeric indexes of arrays (How can I visualize my complex data structure?):

    use warnings; use strict; use Dumpvalue; my $res1 = { 'report' => [ '2011-01-25 10:30:50', { 'AntiVir' => 'Eicar-Test-Signature' } ] }; Dumpvalue->new->dumpValue($res1); __END__ 'report' => ARRAY(0x60b0f0) 0 '2011-01-25 10:30:50' 1 HASH(0x60b220) 'AntiVir' => 'Eicar-Test-Signature'

    An alternate approach is to use Data::Diver:

    use warnings; use strict; use Data::Diver qw(Dive); my $res1 = { 'report' => [ '2011-01-25 10:30:50', { 'AntiVir' => 'Eicar-Test-Signature' } ] }; print Dive( $res1, qw( report 0 ) ), "\n"; print Dive( $res1, qw( report 1 AntiVir ) ), "\n"; __END__ 2011-01-25 10:30:50 Eicar-Test-Signature
Re: Pulling a hash value in a data structure
by AnomalousMonk (Canon) on Jan 25, 2011 at 18:24 UTC

    I'm guessing you didn't use warnings and strictures (see strict and warnings) in your code. If you had, you would have received a message like 'Can't use string ("2011-01-25 10:30:50") as a HASH ref while "strict refs" in use at script name line number', which might have pointed you in the right direction. As you are new to Perl or programming, use diagnostics as well.

    At the start of your program:

    use warnings; use strict; use diagnostics;
Re: Pulling a hash value in a data structure
by biohisham (Priest) on Jan 25, 2011 at 18:00 UTC
    You guessed it right, it is a hash within an array within a hash and so on, such constructs are collectively termed data structures in Perl, read perldsc, perlref and the quick tutorial Arrays and hashes only with references.

    Knowing and mastering these structures will be very handy when you wanna do text processing and data munging tasks..

    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2016-02-14 02:54 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (455 votes), past polls