我正在使用 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/