Arrays 如何获取散列数组中的元素数?
我有一个包含以下数据的哈希数组:Arrays 如何获取散列数组中的元素数?,arrays,perl,hash,Arrays,Perl,Hash,我有一个包含以下数据的哈希数组: $result->{data} = [ { 'SubFeatured' => '0', 'SubHearingImpaired' => '0', 'SubAuthorComment' => undef, 'SubDownloadLink' => 'http://dl.opensubtitles.org/en/downl
$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}
它打印出数组(0x35cedf8)
我对散列以及如何正确使用它们知之甚少 您必须取消对该数组引用的引用:
$foo = @{ $result->{data} };
或
如果您想要
$results->{data}
数组ref中的元素数,您已经很接近了,但是您只需要使用@{}
操作符对数组进行排序
如果需要数组中(每个)hash ref中的元素数,可以使用类似的
%{}
运算符(在本例中,只需一个普通的%$\ucode>即可)
这两个都是:
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
我有一个散列数组
不,您有一个数组引用。被引用数组中的每个元素都是哈希引用
Top level arrays: 3
Hash elements: 43
Hash elements: 43
Hash elements: 43