Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
XP is just a number
 
PerlMonks  

Getting values from hash

by danny0085 (Sexton)
on Apr 26, 2012 at 14:12 UTC ( #967363=perlquestion: print w/ replies, xml ) Need Help??
danny0085 has asked for the wisdom of the Perl Monks concerning the following question:

I am having problems accessing the element name of the trend. I don't know how to do it I tried


for my $search ( @{$result->{trends}} ) but I get the error "Not a HASH reference"


$VAR1 = [ { 'locations' => [ { 'woeid' => 1, 'name' => 'Globales' } ], 'created_at' => '2012-04-26T13:42:24Z', 'trends' => [ { 'events' => undef, 'query' => '%22A%20Picture%20Of%20Me%20Whe +n%20I%20Was%22', 'promoted_content' => undef, 'name' => 'A Picture Of Me When I Was', 'url' => 'http://twitter.com/search/%s%22' }, ], 'as_of' => '2012-04-26T13:46:47Z' } ];

Comment on Getting values from hash
Download Code
Re: Getting values from hash
by Anonymous Monk on Apr 26, 2012 at 14:20 UTC
    $result is not a hash reference. It's an array reference, note the outermost square brackets. Access first the array index, then the hash key, thus:
    for my $search (@{ $result->[0]{trends} })

      Thank you so much, it worked fine.

      Every day I learn a bit more about perl thanks to gurus like you :)

Re: Getting values from hash
by Anonymous Monk on Apr 26, 2012 at 14:20 UTC

    Say it with me, hashes are { curly }arrays are [ square ]

    $result is an array

Re: Getting values from hash
by FloydATC (Hermit) on Apr 26, 2012 at 17:40 UTC
    If a variable isn't the type you expect it to be, try to simply print it; your $result would look something like ARRAY(0x62a964).

    -- Time flies when you don't know what you're doing
Re: Getting values from hash
by Anonymous Monk on Apr 26, 2012 at 18:09 UTC

    The ref() command will help you to find out what type of reference you are referring to.

    Example:

    $result = ['a','b','c','d']; print ref($result);

    This will print as "ARRAY".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2013-06-19 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many continents have you visited?









    Results (643 votes), past polls