>>>> Depending on whether there are one or more sets of values, XML::Simple
>>>> either creates a hash or an array, either of which are stored here:
>>>>
>>>> $parsed_response->{Label}->{Image}
>>>>
>>>> My code retrieves the correct value when it's an array but I'm not
>>>> sure how to do the same when it's a hash.  I also need to be able to
>>>> decide if it's a hash or an array so I can execute the appropriate
>>>> code for retrieving the value.  Can anyone show me how to do this?
>>>
>>> I'm afraid XML::Simple isn't very good at producing consistent data
>>> structures unless it is tweaked a little. Using ForceArray should help;
>>> this is from the documentation
>>>
>>>>    *   check out "ForceArray" because you'll almost certainly want to
>>>> turn
>>>>        it on
>>>
>>> and
>>>
>>>>  ForceArray =>  1 *# in - important*
>>>>    This option should be set to '1' to force nested elements to be
>>>>    represented as arrays even when there is only one.
>>
>> Thanks Rob.  I think ForceArray is what I need but I can't come up
>> with the right setting.  If I set 'ForceArray =>  1', everything seems
>> to be forced into an array but I don't want to loop with foreach for
>> every value I want to retrieve.  I'd like to force only
>> $parsed_response->{Label}->{Image} into an array but I can't find the
>> correct setting for ForceArray.
>
> I presume you have tried
>
>  ForceArray => ['Image']

I tried that and it seems to behave the same as 'ForceArray => 1'.
Everything seems to be forced into an array.

> For further help you must show more of your source XML and examples of
> the two forms of Perl data structure that you are seeing.

I'm very happy to report that I got it working by using 'ForceArray =>
1' and specifying [0] to pick the first (and what should be the only)
value from all of the new arrays.  I'm now using
$parsed_response->{Label}[0]->{Image} in the foreach loop.  Is that a
good solution?

- Grant

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to