迭代hashref arrayref

时间:2015-05-13 02:03:28

标签: arrays perl hashref

我有一个嵌套的数据结构,我想迭代,但我的代码似乎没有工作。没有显示错误。我可以访问这些值的唯一方法是Data::Dumper

my $statuses = $nt->followers_ids({ screen_name => 'arty00l' });

for $statuses ( keys %HoA ) {
    print "$statuses: @{ $HoA{$statuses} }\n";
}

当我使用Data::Dumper时,它会返回:

$VAR1 = {
      'next_cursor_str' => '0'
      'previous_cursor_str' =>
      'next_cursor' => 0,
      'ids' => [
                 98986124,
                 2325887071,
                 2973383805,
                 445207709,
                 316396146,
                 445684239,
                 1265153311,
                 1571662172,
                 2837484211,
                 2881634626,
                 3199312306,
                 2474691877,
                 118090760,
                 2311971607,
                 2298622765,
                 993360248,
                 539085432,
                 52893302,
                 2268568237,
                 20957562,
                 839072018,
                 112728024,
                 140079968,
                 33923443,
                 1450282772,
                 891377682,
                 274127782,
                 284372072,
                 772748041,
                 440211079,
                 30327403,
                 2524695894,
                 354150373,
                 2292575056,
                 68891742,
                 2750381484,
                 316181803,
                 518111226,
                 239159047,
                 284729726,
                 334984455,
                 147508104,
                 159389938,
                 2740303325,
                 212023751,
                 395112717,
                 26411861,
                 172639836,
                 2288625106,
                 24906328,
                 358116535,
                 1493819347,
                 2502142927,
                 30223189,
                 259649352,
                 1375294206,
                 71677965,
                 159632801,
                 24352067,
                 2412589867,
                 1258256690,
                 130765107,
                 1391278070,
                 503718605,
                 1710629294,
                 859406618,
                 362707700,
                 233370937,
                 1707976981,
                 1612009542,
                 407474584,
                 2343960014,
                 237479477,
                 53574764,
                 947969479,
                 276289027,
                 579430568,
                 2474917902,
                 1541546262,
                 2241494730,
                 339179937,
                 77080603,
                 824308147,
                 2434583982,
                 2433258235,
                 1541460948,
                 492471146,
                 2356642530,
                 2327984822,
                 19464956,
                 11245502,
                 14469397,
                 167661601,
                 154072320,
                 167781199,
                 552094012,
                 130158658,
                 1613786857,
                 336603208,
                 32135630,
                 415425580,
                 350058703,
                 65996058,
                 12874852,
                 280884837,
                 106834295,
                 17393362,
                 236185268,
                 62970600,
                 311476854,
                 2289008047,
                 18566247,
                 121598314,
                 1002657601,
                 460224943,
                 376635771,
                 1365484814,
                 2256281789,
                 26632199,
                 2255441468,
                 465013083,
                 1571678467,
                 357547927,
                 259989366,
                 1500479640,
                 19276719,
                 138261007,
                 1030773654,
                 70493313,
                 1406767260,
                 1052462281,
                 72996112,
                 806695453,
                 13027572,
                 555957901,
                 15481158,
                 576112079,
                 920396502,
                 127750483,
                 38542781,
                 42609957,
                 786004562,
                 18289572,
                 412065028,
                 618501933,
                 157882740,
                 952753260,
                 66465341,
                 620076440,
                 23841368,
                 79809731,
                 82091271,
                 311539323,
                 948095714,
                 17191125,
                 804631266,
                 942721417,
                 20525016,
                 403509765,
                 380994873,
                 962970726,
                 182120373,
                 24739893,
                 924945650,
                 36129150,
                 35599004,
                 18068152,
                 189730793,
                 382350502,
                 14910806,
                 84141876,
                 15931491,
                 390450302,
                 850374512,
                 889168250,
                 444940567,
                 58422750,
                 206862729,
                 592305280,
                 225529356,
                 339779020,
                 15957929,
                 587465971,
                 18627463,
                 17485416,
                 715927987,
                 18104730,
                 866372191,
                 564024328,
                 54039049,
                 30588766,
                 124058421,
                 201760549,
                 221005474,
                 82243231,
                 60500502,
                 564774340,
                 564769748,
                 579311942,
                 608952513,
                 509637742,
                 606863740,
                 594839703,
                 151615289,
                 216747794,
                 176608284,
                 635013126,
                 249857095,
                 321985491,
                 18805118,
                 112757114,
                 111065326,
                 111234967
               ],
      'previous_cursor' => 0
    };

此行后代码应该是什么样的?

my $statuses = $nt->followers_ids({ screen_name => 'arty00l'})

2 个答案:

答案 0 :(得分:1)

首先,始终使用use strict; use warnings;。它会更早,更明显地捕捉到您的错误。

关于这个问题。您没有指定要迭代的内容。您将变量" status和#34;命名为,但在变量的输出中没有任何类似的状态。据推测,您想输出ID。为此,请使用

print "@{ $VAR1->{ids} }\n";

如果$VAR1是对%HoA的引用,则代码实际上是

print "@{ $HoA{ids} }\n";

答案 1 :(得分:0)

你说

  

当我使用Data :: Dumper时,它返回

但你不能说 你使用Data::Dumper的方式或者你正在倾倒的内容

您展示的代码不会使用followers_ids的返回值执行任何操作。此行将值分配给$statuses

my $statuses = $nt->followers_ids({ screen_name => 'arty00l' });

被这个循环立即丢弃和覆盖,该循环使用相同的变量

for $statuses ( keys %HoA ) {
    print "$statuses: @{ $HoA{$statuses} }\n";
}

并且在您提供更多信息之前,我们不能说明%HoA是什么,我们可以帮助您进一步提升

根据module documentationfollowers_ids方法

  

返回对指定用户

之后的每个用户的数字ID数组的引用

这意味着要显示其内容所需要做的就是写

my $statuses = $nt->followers_ids({ screen_name => 'arty00l' });
print "$_\n" for @$statuses;

但是这只会给你一个ID列表

如果你详细说明你想做什么,那么我相信我们可以帮助你更好