perlperl-hash

How do I decipher an array of hashes?


I totally got this question wrong. Am using the method from TMDB:

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

I thought the output was in JSON format, so that's what confused me, which kept me running in circles because I was looking at it all wrong.

Didn't realize the data below, from Dumper, was the actual hashes the I had to go through.

This is where I am running into a wall, So the data below is a hash with five keys. The fifth key, the I want, contains another array. It is that array I cannot read into. I try dereferencing that into a hash, and that is where I fail.

The code I am trying is:

foreach my $narray (@results){
    print $narray->{"movie_results"};
    my @newarray = $narray->{"movie_results"};

    foreach my $otherarray (@newarray){
        my %innerhash = $otherarray;
        print %innerhash;
        print "\n";
    }
  } 

It will print out an array, but I am unable to read the hash in that array.

p.s. I had to format this output as code, or else it came out with no line breaks.

   $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 business starts tanking, it\'s up to Tommy and number-cruncher Richard Hayden to save the day.',
                                     'genre_ids' => [
                                                      35
                                                    ],
                                     'title' => 'Tommy Boy',
                                     'video' => $VAR1->{'movie_results'}[0]{'adult'},
                                     'poster_path' => '/g32WbO9nbY5ydpux5hIoiJkLEQi.jpg',
                                     'original_language' => 'en',
                                     'backdrop_path' => '/bZ4diYf7oyDVaRYeWG42Oify2mB.jpg',
                                     'popularity' => '13.945'
                                   }
                                 ]
            };

Solution

  • When dealing with reference, use the same syntax as if you weren't, but replace the name of the variable with a block that returns the reference.

    %NAME      -> %{ $ref }           Or just %$ref
    $NAME{...} -> ${ $ref }{...}      Although $ref->{...} easier to read.
    
    @NAME      -> @{ $ref }           Or just @$ref
    $NAME[...] -> ${ $ref }[...]      Although $ref->[...] easier to read.
    

    Let's give $VAR a better name,

    my $response = $VAR1;
    

    This means you want

    my $results = $response->{movie_results};
    for my $result (@$results) {
       for my $key (keys(%$result)) {
          say "$key: $result->{$key}";
       }
    }
    

    See