On 15/06/2011 18:02, Grant wrote:
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.
Alternatively, you can check whether you are dealing with a reference to
an array or to a hash by using the 'ref' operator, which will return
'ARRAY' or 'HASH' respectively. For instance
for ([], {}) {
print ref, "\n";
}
prints
ARRAY
HASH
I hope this helps,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/