perl - 如何循环访问hashref?

时间:2016-06-10 12:39:14

标签: perl foreach hashref

我有一些hashref格式的数据。我从graph.facebook.com获取数据 如何循环访问?

$var = \{
            'data' => [
                        {
                          'id' => '312351465029_10154168935475030',
                          'name' => 'Timeline Photos 1'
                        },
                        {
                          'name' => 'Bangchak\'s cover photo',
                          'id' => '312351465029_10154168087455030',
                        },
                        {
                          'id' => '312351465029_10154168081875030',
                          'name' => 'Timeline Photos 2',
                        }
                      ],
            'paging' => {
                          'previous' => 'https://graph.facebook.com/v2.6/312351465029/2',
                          'next' => 'https://graph.facebook.com/v2.6/312351465029/3'
                        }
          };

这些代码无效。

foreach $m ($var->{data})
{
    if ( $m->{name} =~ /Timeline/i )
    {
        print "id = $m->{id}\n";
    }
}

1 个答案:

答案 0 :(得分:2)

您需要取消引用数组(perldoc perldsc):

import marshal
# code ...
string1 = marshal.dumps(sp1)