我有一个包含以下数据的哈希数组:
$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'
}
];
我的代码要求我获得数组$result->{data}
中的元素数量。但是我不确定这样做的正确方法。
我试过了:
scalar $result->{data}
打印出ARRAY(0x35cedf8)
我对哈希知之甚少,以及如何正确使用它们。
答案 0 :(得分:4)
您必须取消引用该数组引用:
$foo = @{ $result->{data} };
或
print scalar @{ $result->{data} };
答案 1 :(得分:2)
如果你想要$results->{data}
数组ref中的元素数量,那么你就近了,但你只需要用@{ }
运算符来解析数组。
如果你想要在数组中的(每个)散列引用中的元素数量,你可以使用类似的%{ }
运算符(在这种情况下只是一个普通的%$_
)来完成它。
以下是:
printf "Top level arrays: %2d\n", scalar @{$result->{data}};
printf " Hash elements: %2d\n", scalar keys %$_ for @{$result->{data}};
输出:
Top level arrays: 3
Hash elements: 43
Hash elements: 43
Hash elements: 43
答案 2 :(得分:0)
我有一个哈希数组
没有。你有一个数组引用。引用数组中的每个元素都是一个哈希引用。