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' } ];

Replies are listed 'Best First'.
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 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".

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 (Deacon) 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