Perl 打印哈希数组的数组?

标签 perl twitter multidimensional-array hashtable

我正在使用 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',
      }
    ];

最佳答案

我查看了 Net::Twitter,它本质上非常简陋。它会将您引回到 API,并且几乎让您留在那儿 —— 主要是因为 API 不断变化,这使得很难生成稳定的模块。大多数模块都试图清理对散列数组的引用对数组散列的引用的困惑,但这个模块没有。

看起来您必须使用 Data::Dumper 来查看返回的数据结构,并尽可能地解析它。

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

关于Perl 打印哈希数组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11622615/

相关文章:

perl - grep 在大文件上表现不佳,有没有替代方法?

perl - 如何使用 Perl 的 XML::Twig 选择同级(xpath 语法)?

Perl双点范围运算符.. vs三点...

python - 转推时 Tweepy 未经授权的 401 错误

python - 通过 Python 抓取 Twitter 嵌入 URL

matlab - 在未知维度的矩阵中使用冒号进行索引

php - PHP 中的多维数组、乘法和加法

php - 使用 PHP 和 MySQL 从多维关联数组插入到多个表中

mysql - DBD::MySql 初始化失败:使用生物信息学工具 OrthoMCL 时无法通过包 "driver"定位对象方法 "DBD::MySql"

javascript - 如何在 Twitter 嵌入式时间线刷新之前设置超时?