将格式化数据推送到Perl中的数组

时间:2015-03-31 15:36:58

标签: perl multidimensional-array

我有一串格式化的数据,我想推送到一个数组,但我的Perl技能缺乏。

字符串是:

'ShoreTelCallStateInfo' => [
    {
       'callStateDetail' => 'Active',
       'callState' => 'OnHold',
       'callInfo' => {
             'callerIDName' => 'Joel Lewis',                                          
             'callID' => '66766',
             'lineID' => '3947',
             'connectedIDName' => 'VM-Forward',
             'calledID' => '2105',
             'callerID' => '1955',
             'isInbound' => 'false',
             'calledIDName' => 'VM-Forward',
             'callReason' => 'None',
             'callUniqueID' => '2488927099',
             'connectedID' => '2105',
             'isExternal' => 'false',
             'callGUID' => '{00030000-67CA-537E-3FD8-0010492377D9}'
        }
    },
    {
        'callStateDetail' => 'Active',
        'callState' => 'Connected',
        'callInfo' => {
             'callerIDName' => 'Lewis Joel',
             'callID' => '73202',
             'lineID' => '3947',
             'connectedIDName' => 'Lewis Joel',
             'calledID' => '1955',
             'callerID' => '+1385#######',
             'isInbound' => 'true',
             'calledIDName' => 'Joel Lewis',
             'callReason' => 'None',
             'callUniqueID' => '2193468845',
             'connectedID' => '+1385#######',
             'isExternal' => 'true',
             'callGUID' => '{00030000-6809-537E-3FD8-0010492377D9}'
        }
     }
  ]
};

我试图简单地创建数组并分配字符串,但这不起作用:

my @magicarray = $string;

是否有使用格式化数据初始化此数组的快速方法?

2 个答案:

答案 0 :(得分:2)

只需添加丢失的左大括号并调用eval。在此之前,请确保字符串不包含任何命令(想象system 'rm -rf /'会导致什么)。

my $string = q( 'ShoreTelCallStateInfo' => [
                                     {
                                       'callStateDetail' => 'Active',
# ETC...
                                     }
                                   ]
        };);
my @array = eval "{$string";

答案 1 :(得分:1)

如果不更好地理解数据,很难提供帮助。但我建议这个

my $state_info = $result->{ShoreTelCallStateInfo};

for my $state_item ( @$state_info ) {
  say $state_item->{callInfo}{callerID};
}

使用样本数据时

1955
+1385#######

这接近你想要的吗?