in reply to Having trouble reading in a hash from a referenced array

your question is a little fuzzy, but if you wanna access something like vote_average directly you need

$VAR1->{movie_results}[0]{vote_average} because the value of movie_results is an array of hashes.

$VAR1 = { ... 'movie_results' => [ { ... 'vote_average' => '6.8', ...

see perldsc about nested data structures,

I suppose you can have multiple results for the same query, that's why it's an AoH.

And if you just wanna have the AoH try

my $a_movie_results = $VAR1->{"movie_results"}; as a array_ref

or expanded to a list

my @movie_results = @{$VAR1->{"movie_results"}}; as an array

update

likewise:

my $h_first_result = $VAR1->{"movie_results"}[0]; as a hash_ref

or expanded to a list

my %first_result   = %{ $VAR1->{"movie_results"}[0] }; as a hash

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Having trouble reading in a hash from a referenced array
by bliako (Abbot) on Dec 18, 2020 at 22:54 UTC

    Wouldn't using $VAR1->{"movie_results"}->[0] make it clearer that key/entry "movie_results" contains an array reference? $VAR1->{"movie_results"}[0] is confusing (to me). Why is it even allowed to work? It begs me to turn to a manual for clarification about what data types a Perl hash can contain. Sanity check: Perl Data Structures still hold only numbers, scalars and references. OK. Personally, I like to see this extra clarification (the ->) in my (and others) code. It makes reading and understanding (other's) code faster, albeit it seems like a sticking finger!!!.

    bw, bliako

      > Why is it even allowed to work?

      It's the same and documented so, after the first arrow the others are redundant

      $VAR1->{"movie_results"}->[0]

      exactly the same as

      $VAR1->{"movie_results"}[0]

      will update with links to perldocs ...

      UPDATE

      see perlreftut#Arrow-Rule

      "In between two subscripts, the arrow is optional."

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Quite right you are.

        But I feel that making the arrow optional between two subscripts is confusing if Perl still makes a distinction between a reference and the data structure it refers to. I guess the rule is simple: PDS (Perl Data Structure=hash,array) can not contain arrays but refs or scalars. So drop the arrow notation. Hmmmm that might be confusing for a new-comer as it may suggest that arrays (not arrayref) is allowed inside a PDS.

        That's a general Perl comment, not about what you wrote.