简体   繁体   中英

Pushing formatted data to an array in Perl

I have a string of formatted data that I would like to push to an array, but my Perl skills are lacking.

The string is:

'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}'
        }
     }
  ]
};

I have tried to simply create the array and assign the string, but this is not working:

my @magicarray = $string;

Is there a quick way to initialize this array with the formatted data?

Just prepend the missing left curly bracket and call eval . Before doing so, make sure the string doesn't contain any commands (imagine what system 'rm -rf /' would cause).

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

It is difficult to help without understanding the data better. But I suggest this

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

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

when worked with your sample data gives

1955
+1385#######

Is that close to what you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM