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

Dear Perl Monks,

I am running into a wall when I use TMDB's

my @results = $search->find( id => 'tt0114694', source => 'imdb_id' );

The result I get back is a hash that comes back in a referenced array. in my case it's just one item returned, which is posted at the bottom. When I read the fifth key in the hash, I get an array. I am assumed that it is also an array of another hash. Obviously it is not, and there is where I am stuck.

When I use this code, I get the same array for both. If someone can tell me what I am getting wrong, I'd appreciate it so much.

foreach my $narray (@results){ print $narray->{"movie_results"}; print "\n"; my @newarray = $narray->{"movie_results"}; print $newarray[0]; print "\n";

This is my output from the original call

$VAR1 = { 'tv_season_results' => [], 'tv_results' => [], 'person_results' => [], 'tv_episode_results' => [], 'movie_results' => [ { 'adult' => bless( do{\(my $o = 0) +}, 'JSON::PP::Boolean' ), 'vote_average' => '6.8', 'original_title' => 'Tommy Boy', 'vote_count' => 635, 'id' => 11381, 'release_date' => '1995-03-31', 'overview' => 'Party animal Tommy + Callahan is a few cans short of a six-pack. But when the family busi +ness starts tanking, it\'s up to Tommy and number-cruncher Richard Ha +yden to save the day.', 'genre_ids' => [ 35 ], 'title' => 'Tommy Boy', 'video' => $VAR1->{'movie_results +'}[0]{'adult'}, 'poster_path' => '/g32WbO9nbY5ydp +ux5hIoiJkLEQi.jpg', 'original_language' => 'en', 'backdrop_path' => '/bZ4diYf7oyDV +aRYeWG42Oify2mB.jpg', 'popularity' => '13.945' } ] };

Replies are listed 'Best First'.
Re: Having trouble reading in a hash from a referenced array
by dave_the_m (Monsignor) on Dec 18, 2020 at 09:01 UTC
    AT a guess, you need to replace this:
    my @newarray = $narray->{"movie_results"};
    with this:
    my @newarray = @{$narray->{"movie_results"}};
    The first sets $newarray[0] to the same array ref contained in the "movie_results" slot of the hash; the second dereferences that array ref and returns all the elements in that array.

    Dave.

Re: Having trouble reading in a hash from a referenced array
by LanX (Saint) on Dec 18, 2020 at 13:57 UTC
    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

      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

Re: Having trouble reading in a hash from a referenced array
by perlfan (Parson) on Dec 18, 2020 at 17:31 UTC
    You're getting a hash reference, so something like the following would make it more natural for you:
    my $results_href = $search->find( id => 'tt0114694', source => 'imdb_id' ); my $movies_aref = $results_ref->{movie_results}; printf(qq{~~~~%d M O V I E R E S U L T S~~~~\n}, scalar @{$movies_ar +ef}) if @{$movies_aref}; foreach my $movie_href (@{$movies_aref}) { $title = $movie_href->{title}; $summary = $movie_href->{summary}; print qq{ Title: $title Summary: $summary ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }; } print qq{~~~~N O M O V I E R E S U L T S :( :(~~~~\n} if @{$movies_a +ref};
Re: Having trouble reading in a hash from a referenced array
by bliako (Abbot) on Dec 18, 2020 at 23:07 UTC

    A Perl Data Structure can contain only numbers, scalars and references (for example refs to other Perl Data Structures or scalars etc). That's it.

    Use the square brackets notation to declare an array reference my $arrayref = [1,2,3]. You should use my @x = @{ $arrayref }; to dereference the reference back into a new array. You can skip dereferencing into an array by using the $arrayref->[0] notation to access individual array elements.

    bw, bliako