Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Parsing SOAP::Lite results

by kcott (Abbot)
on Sep 28, 2012 at 12:05 UTC ( #996189=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Parsing SOAP::Lite results
in thread Parsing SOAP::Lite results

I tested that code before posting. I changed $VAR1 to my $getToysResults but otherwise left the Dumper output unchanged. Here's the script (pm_parse_soap_hash.pl) in its entirety:

#!/usr/bin/env perl use strict; use warnings; my $getToysResults = { 'Toys' => bless( { 'Toy' => [ bless( { 'ToyLocations' => bless( { 'ToyLocation' => [ { 'toyQuantity' => '1', 'locationName' => 'toybox' }, { 'toyQuantity' => '4', 'locationName' => 'shelf' } ] }, 'ArrayOfToyLocation' ), 'color' => 'none', 'toyName' => 'Sorry', 'size' => 'medium' }, 'Board' ) ] }, 'ArrayOfToy' ) }; foreach my $e (@{$getToysResults->{Toys}{Toy}}) { my $location_array_ref = $e->{ToyLocations}{ToyLocation}; for my $location_array_hash_ref (@$location_array_ref) { print $location_array_hash_ref->{locationName}, "\n"; } }

Here's the output:

$ pm_parse_soap_hash.pl toybox shelf

Try running that script. If it works, then maybe the data you posted was incorrect (typo, cut-and-paste error, etc.) or perhaps you used my code differently. Try adding print Dumper $getToysResults; just before the outer foreach in your code and see if it produces the same output as you originally posted.

If it doesn't work, please post full output along with your Perl version and OS: as it stands, I can't see anything there that wouldn't work on any version of Perl 5.

-- Ken


Comment on Re^3: Parsing SOAP::Lite results
Select or Download Code
Re^4: Parsing SOAP::Lite results
by Roboz (Novice) on Sep 28, 2012 at 12:52 UTC

    I see what's causing my issue with the 'Not an ARRAY ref' error. When there is only one location it's not an array.

    $VAR1 = { 'Toys' => bless( { 'Toy' => [ bless( { 'ToyLocations' => bless( { 'ToyLocation' => { 'toyQuantity' => '2', 'locationName' => 'toy +box' } }, 'ArrayOfToyLocation' ), 'color' => 'brown', 'toyName' => 'bear', 'size' => 'large' }, 'Stuffed' ), bless( { 'ToyLocations' => bless( { 'ToyLocation' => [ { 'toyQuantity' => '1', 'locationName' => 'toy +box' }, { 'toyQuantity' => '4', 'locationName' => 'she +lf' } ] }, 'ArrayOfToyLocation' ), 'color' => 'none', 'toyName' => 'Sorry', 'size' => 'medium' }, 'Board' ) ] }, 'ArrayOfToy' ) };

    Still stuck on what to do in this case. Would this be where introspection comes in?  pseudo:(if HASH, then ... %$location) elsif ARRAY, then ... @$location)  ? Thanks to all who are helping me learn this!

      Yes, ref is ref

      You can use ref to do that.

      Here's a modification of my earlier script to show this. For illustration purposes, I've made minor changes to the locationName values so you can see which toybox is being referred to.

      #!/usr/bin/env perl use strict; use warnings; my $getToysResults = { 'Toys' => bless( { 'Toy' => [ bless( { 'ToyLocations' => bless( { 'ToyLocation' => { 'toyQuantity' => '2', 'locationName' => 'HASH:toybox +' } }, 'ArrayOfToyLocation' ), 'color' => 'brown', 'toyName' => 'bear', 'size' => 'large' }, 'Stuffed' ), bless( { 'ToyLocations' => bless( { 'ToyLocation' => [ { 'toyQuantity' => '1', 'locationName' => 'ARRAY:toybo +x' }, { 'toyQuantity' => '4', 'locationName' => 'ARRAY:shelf +' } ] }, 'ArrayOfToyLocation' ), 'color' => 'none', 'toyName' => 'Sorry', 'size' => 'medium' }, 'Board' ) ] }, 'ArrayOfToy' ) }; foreach my $e (@{$getToysResults->{Toys}{Toy}}) { my $location_ref = $e->{ToyLocations}{ToyLocation}; my $location_array_ref = ref($location_ref) eq 'ARRAY' ? $location_ref : [ $location_ +ref ]; for my $location_array_hash_ref (@$location_array_ref) { print $location_array_hash_ref->{locationName}, "\n"; } }

      Output:

      $ pm_parse_soap_ref.pl HASH:toybox ARRAY:toybox ARRAY:shelf

      -- Ken

        That worked great. I had tried some  if (ref($location_ref) eq 'ARRAY') {...} elsif (ref($location_ref) eq 'HASH') {...} and just changed the @$ to a %$ and was getting a 'Can't use string ... strict refs' error. Glad you returned with the better way to do it.

        Can you tell me what's happening here?

        my $location_array_ref = ref($location_ref) eq 'ARRAY' ? $location_ref : [ $location_ +ref ];

        Thank you very much!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996189]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-12-28 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls