0

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?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
June Lewis
  • 355
  • 1
  • 6
  • 28
  • That's not a string. If you add a `{` to the beginning, it would be an anonymous hash. Maybe you meant a "scalar"? – choroba Mar 31 '15 at 15:43
  • It doesn't look like a string to me – Hunter McMillen Mar 31 '15 at 15:43
  • It is stored as a string, I want it to be converted. – June Lewis Mar 31 '15 at 15:45
  • But it might be related to a SOAP object: http://stackoverflow.com/questions/29370434/how-to-loop-through-subarrays-of-a-soaplite-response-in-perl – Sobrique Mar 31 '15 at 15:45
  • Correct sobrique, but since I can't get what I need out of it as a soap object I would like to convert it to a array. I currently have it as a string as well. – June Lewis Mar 31 '15 at 15:46
  • What do you need out of the SOAP object? Alternatively, how would storing this in an array enable you to get the information you need? – Hunter McMillen Mar 31 '15 at 15:51
  • @choroba: *"That's not a string"* It looks very much like a string to me. I would be hard-pressed to imagine something that *didn't* look like a string! – Borodin Mar 31 '15 at 16:01
  • 2
    @JoelLewis: You're asking the wrong question again! I tried to explain that it is almost never useful to print the `Data::Dumper` representation of an object. Most of the information is in the *methods* that the object provides. Please explain what you're trying to do using the SOAP API, and give example sources if possible so that we can test solutions ourselves – Borodin Mar 31 '15 at 16:04
  • Can you show us the bigger picture please? You contracted our view way too small so that all we have is the output of [`Data::Dumper`](https://metacpan.org/module/Data::Dumper). Please show your complete solution and your over-all requirement – Borodin Mar 31 '15 at 16:43
  • @JoelLewis: How about you expand this in a new question, and say when you're not sure: I wrote a SOAP client in Perl that uses the `SOAP::Lite` module. I wrote to send a request to the SOAP server, and to display what the server returned. Please include as much information as you can – Borodin Mar 31 '15 at 17:11
  • @borodin I added all of that to the other question https://stackoverflow.com/questions/29370434/how-to-loop-through-subarrays-of-a-soaplite-response-in-perl I thought it was more appropriate to add to the question that was titled correctly. – June Lewis Mar 31 '15 at 17:13

2 Answers2

2

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";
choroba
  • 231,213
  • 25
  • 204
  • 289
1

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?

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • This is exactly what I'm looking for, however for some reason $state info is blank. Resulting in no loop. I can't figure out why state info is not getting the data. Also I think you posted this to the wrong question of mine. – June Lewis Mar 31 '15 at 16:42
  • 1
    I apologise for posting against the wrong question, but your new post was very similar to this one. Please pull back a long way and explain the nature of the data source as well as why you are using `Data::Dumper` to serialise, and then `eval` to deserialise. You motives may be valid but it is impossible to tell from what you have written. I realise that many things seem obvious to you, but are part of the system with which you are working. We cannot knowany of that, so please imagine that we are from Jupiter and know programming but nothing of your system – Borodin Mar 31 '15 at 16:48
  • Thank you for being patient and trying to help. I have updated my origional question: https://stackoverflow.com/questions/29370434/how-to-loop-through-subarrays-of-a-soaplite-response-in-perl I hope it is a bit easier to understand what is going on. – June Lewis Mar 31 '15 at 16:57