【问题标题】:Perl printing array of array of hashes?Perl打印哈希数组?
【发布时间】:2012-07-22 06:49:04
【问题描述】:

我正在使用 Net::Twitter 访问我的 Twitter 消息。目前 direct_message() 返回消息数组。我发现它实际上是一个数组数组(基于帐户,然后是每个单独的消息)。

我想知道是否有人知道从结果中打印消息 ID 的更好方法?我只是认为它是一个数组的数组,因为 $msgs[0][0]{'id'} 会打印出消息 ID。当我做数据转储程序时,它看起来更像是一个哈希数组。

foreach my $msg (@{$msgs[0]})
{
    print $msg->{'id'} . "\n";
}

Data::Dumper 结果

$VAR1 = [
      {
        'sender_screen_name' => 'text',
        'recipient' => {
                         'friends_count' => 'text',
                         'follow_request_sent' => 'text',
                         'profile_background_image_url_https' => 'text',
                         'profile_sidebar_fill_color' => 'text',
                         'profile_image_url' => 'text',
                         'profile_background_color' => 'text',
                         'notifications' => 'text',
                         'url' => 'text',
                         'id' => 'text',
                         'is_translator' => 'text',
                         'following' => 'text',
                         'screen_name' => 'text',
                         'lang' => 'text',
                         'location' => 'text',
                         'followers_count' => 'text',
                         'statuses_count' => 'text',
                         'name' => 'text',
                         'description' => 'text',
                         'favourites_count' => 'text',
                         'profile_background_tile' => 'text',
                         'listed_count' => 'text',
                         'contributors_enabled' => 'text',
                         'profile_link_color' => 'text',
                         'profile_image_url_https' => 'text',
                         'profile_sidebar_border_color' => 'text',
                         'created_at' => 'text',
                         'utc_offset' => 'text',
                         'verified' => 'text',
                         'show_all_inline_media' => 'text',
                         'profile_background_image_url' => 'text',
                         'protected' => 'text',
                         'default_profile' => 'text',
                         'id_str' => 'text',
                         'profile_text_color' => 'text',
                         'default_profile_image' => 'text',
                         'time_zone' => 'text',
                         'profile_use_background_image' => 'text',
                         'geo_enabled' => 'text',
                       },
        'id_str' => 'text',
        'sender_id' => 'text',
        'created_at' => 'text',
        'text' => 'text',
        'sender' => {
                      'friends_count' => 'text',
                      'follow_request_sent' => 'text',
                      'profile_background_image_url_https' => 'text',
                      'profile_sidebar_fill_color' => 'text',
                      'profile_image_url' => 'text',
                      'profile_background_color' => 'text',
                      'notifications' => 'text',
                      'url' => 'text',
                      'id' => 'text',
                      'is_translator' => 'text',
                      'following' => 'text',
                      'screen_name' => 'text',
                      'lang' => 'text',
                      'location' => 'text',
                      'followers_count' => 'text',
                      'statuses_count' => 'text',
                      'name' => 'text',
                      'description' => 'text',
                      'favourites_count' => 'text',
                      'profile_background_tile' => 'text',
                      'listed_count' => 'text',
                      'contributors_enabled' => 'text',
                      'profile_banner_url' => 'text',
                      'profile_link_color' => 'text',
                      'profile_image_url_https' => 'text',
                      'profile_sidebar_border_color' => 'text',
                      'created_at' => 'text',
                      'utc_offset' => 'text',
                      'verified' => 'text',
                      'show_all_inline_media' => 'text',
                      'profile_background_image_url' => 'text',
                      'protected' => 'text',
                      'default_profile' => 'text',
                      'id_str' => 'text',
                      'profile_text_color' => 'text',
                      'default_profile_image' => 'text',
                      'time_zone' => 'text',
                      'profile_use_background_image' => 'text',
                      'geo_enabled' => 'text',
                    },
        'recipient_screen_name' => 'text',
        'id' => 'text',
        'recipient_id' => 'text',
      }
    ];

【问题讨论】:

  • 包括 Data-Dumper 输出,也许还有你想看到的格式。
  • @SethRobertson 我已经包含了 Data-Dumper 输出。我只想访问每个消息 ID(从底部开始的第二个“id”键)和消息(在发件人哈希上方的哈希“文本”)。做一个简单的 print $msgs[0][0]{'id'}, $msgs[0][1]{'id'} 会分别给我第一条和第二条消息的 id。

标签: perl twitter multidimensional-array hashtable


【解决方案1】:

我查看了Net::Twitter,它本质上非常骨骼。它把你引回到 API 上,几乎让你留在那儿——主要是因为 API 不断变化,这使得很难产生一个稳定的模块。大多数模块都试图清理对哈希数组的引用对数组哈希的引用混乱,但这个没有。

看来您必须使用Data::Dumper 来查看返回的数据结构,并以最佳方式对其进行解析。

在这种情况下,您可以使用ref 命令来准确验证您正在查看的内容(如果它在您期望对数组的引用时返回对哈希的引用,那么您知道您可能会那样麻烦)。您还可以查看Net::Twitter::Lite,它可能会为基本对象处理提供更多支持。

【讨论】:

  • 是的,我确实使用了 Data::Dumper,但它看起来像一个哈希数组。这就是为什么额外的 [0] 让我失望的原因我不知道是否有比我想出的更好的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 2020-10-16
  • 2013-06-24
  • 1970-01-01
  • 2011-10-27
  • 2016-02-21
  • 1970-01-01
相关资源
最近更新 更多