Reputation: 8061
I have a hash array with the following data:
$result->{data} = [
{
'SubFeatured' => '0',
'SubHearingImpaired' => '0',
'SubAuthorComment' => undef,
'SubDownloadLink' => 'http://dl.opensubtitles.org/en/download/filead/src-api/vrf-308688c2a2/1952675182.gz',
'MovieNameEng' => undef,
'SubDownloadsCnt' => '1836',
'MovieName' => '"Merlin" The Witch\'s Quickening',
'LanguageName' => 'English',
'MovieFPS' => '25.000',
'SeriesIMDBParent' => '1199099',
'MovieHash' => 'a03eeb674b4c355c',
'SeriesEpisode' => '11',
'UserNickName' => undef,
'MovieYear' => '2009',
'SubSize' => '33290',
'MovieKind' => 'episode',
'IDSubMovieFile' => '1852103',
'ZipDownloadLink' => 'http://dl.opensubtitles.org/en/download/subad/src-api/vrf-7920df6fe7/4001829',
'SubBad' => '0',
'ISO639' => 'en',
'IDSubtitle' => '4001829',
'MovieReleaseName' => 'Merlin 02x11 The Witch\'s Quickening DVDRip HAGGiS',
'MatchedBy' => 'moviehash',
'MovieTimeMS' => '0',
'UserID' => '0',
'SubFormat' => 'srt',
'MovieByteSize' => '365428736',
'SubRating' => '10.0',
'SubComments' => '0',
'SubActualCD' => '1',
'SubHash' => '791313d38bfb1eedd5d1c8985b72a7d3',
'UserRank' => undef,
'IDSubtitleFile' => '1952675182',
'SubSumCD' => '1',
'SubFileName' => 'Merlin - 02x11 - The Witch\'s Quickening.DVDRip.HAGGiS.English.orig.Addic7ed.com.srt',
'SubAddDate' => '2010-12-10 20:55:54',
'IDMovieImdb' => '1558034',
'SubtitlesLink' => 'http://www.opensubtitles.org/en/subtitles/4001829/merlin-the-witch-s-quickening-en',
'IDMovie' => '100498',
'SeriesSeason' => '2',
'SubHD' => '0',
'SubLanguageID' => 'eng',
'MovieImdbRating' => '8.1'
},
{
'SubFeatured' => '0',
'SubHearingImpaired' => '0',
'SubAuthorComment' => undef,
'SubDownloadLink' => 'http://dl.opensubtitles.org/en/download/filead/src-api/vrf-69741ece04/1953248198.gz',
'MovieNameEng' => undef,
'SubDownloadsCnt' => '340',
'MovieName' => '"Merlin" The Witch\'s Quickening',
'LanguageName' => 'English',
'MovieFPS' => '25.000',
'SeriesIMDBParent' => '1199099',
'MovieHash' => 'a03eeb674b4c355c',
'SeriesEpisode' => '11',
'UserNickName' => undef,
'MovieYear' => '2009',
'SubSize' => '33286',
'MovieKind' => 'episode',
'IDSubMovieFile' => '5485801',
'ZipDownloadLink' => 'http://dl.opensubtitles.org/en/download/subad/src-api/vrf-e53ed25c1a/4702165',
'SubBad' => '0',
'ISO639' => 'en',
'IDSubtitle' => '4702165',
'MovieReleaseName' => ' merlin.s02e11',
'MatchedBy' => 'moviehash',
'MovieTimeMS' => '0',
'UserID' => '0',
'SubFormat' => 'srt',
'MovieByteSize' => '365428736',
'SubRating' => '0.0',
'SubComments' => '0',
'SubActualCD' => '1',
'SubHash' => '9a4f979c8bf8d1e5167fe2954e94b7c1',
'UserRank' => undef,
'IDSubtitleFile' => '1953248198',
'SubSumCD' => '1',
'SubFileName' => 'merlin.s02e11.srt',
'SubAddDate' => '2012-10-29 18:33:26',
'IDMovieImdb' => '1558034',
'SubtitlesLink' => 'http://www.opensubtitles.org/en/subtitles/4702165/merlin-the-witch-s-quickening-en',
'IDMovie' => '100498',
'SeriesSeason' => '2',
'SubHD' => '0',
'SubLanguageID' => 'eng',
'MovieImdbRating' => '8.1'
},
{
'SubFeatured' => '0',
'SubHearingImpaired' => '0',
'SubAuthorComment' => undef,
'SubDownloadLink' => 'http://dl.opensubtitles.org/en/download/filead/src-api/vrf-025968d0b0/1953259955.gz',
'MovieNameEng' => undef,
'SubDownloadsCnt' => '472',
'MovieName' => '"Merlin" The Witch\'s Quickening',
'LanguageName' => 'English',
'MovieFPS' => '25.000',
'SeriesIMDBParent' => '1199099',
'MovieHash' => 'a03eeb674b4c355c',
'SeriesEpisode' => '11',
'UserNickName' => undef,
'MovieYear' => '2009',
'SubSize' => '33456',
'MovieKind' => 'episode',
'IDSubMovieFile' => '9097729',
'ZipDownloadLink' => 'http://dl.opensubtitles.org/en/download/subad/src-api/vrf-9226ed7194/4714102',
'SubBad' => '0',
'ISO639' => 'en',
'IDSubtitle' => '4714102',
'MovieReleaseName' => ' Merlin 2x11',
'MatchedBy' => 'moviehash',
'MovieTimeMS' => '0',
'UserID' => '0',
'SubFormat' => 'srt',
'MovieByteSize' => '365428736',
'SubRating' => '0.0',
'SubComments' => '0',
'SubActualCD' => '1',
'SubHash' => '07a4a8902093b1e578d46875b214fb27',
'UserRank' => undef,
'IDSubtitleFile' => '1953259955',
'SubSumCD' => '1',
'SubFileName' => 'Merlin 2x11.srt',
'SubAddDate' => '2012-11-12 19:40:13',
'IDMovieImdb' => '1558034',
'SubtitlesLink' => 'http://www.opensubtitles.org/en/subtitles/4714102/merlin-the-witch-s-quickening-en',
'IDMovie' => '100498',
'SeriesSeason' => '2',
'SubHD' => '0',
'SubLanguageID' => 'eng',
'MovieImdbRating' => '8.1'
}
];
My code requires that I get the number of elements in the array $result->{data}
. However I am unsure of the proper method of doing so.
I tried:
scalar $result->{data}
which prints out ARRAY(0x35cedf8)
I know little about hashes, and how to properly use them.
Upvotes: 2
Views: 246
Reputation: 69274
I have a hash array
No. You have an array reference. Each element within the referenced array is a hash reference.
Upvotes: 0
Reputation: 635
If you want the number of elements in the $results->{data}
array ref, you were close, but you just need to deref the array with the @{ }
operator.
If you want the number of elements in (each) hash ref within the array, you can accomplish that with the similar %{ }
operator (just a plain %$_
in this case).
Here are both:
printf "Top level arrays: %2d\n", scalar @{$result->{data}};
printf " Hash elements: %2d\n", scalar keys %$_ for @{$result->{data}};
This outputs:
Top level arrays: 3
Hash elements: 43
Hash elements: 43
Hash elements: 43
Upvotes: 2
Reputation: 5290
You have to dereference that array reference:
$foo = @{ $result->{data} };
or
print scalar @{ $result->{data} };
Upvotes: 4