Hash or array & retrieving hash value

Hash or array & retrieving hash value

am 15.06.2011 19:02:48 von grant

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?

- Grant

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

Re: Hash or array & retrieving hash value

am 15.06.2011 19:12:20 von Rob Dixon

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Hash or array & retrieving hash value

am 16.06.2011 03:15:12 von grant

>> 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. =A0I 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. =A0Can 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
>
>> =A0 =A0* =A0 check out "ForceArray" because you'll almost certainly want=
to turn
>> =A0 =A0 =A0 =A0it on
>
> and
>
>> =A0ForceArray =3D> 1 *# in - important*
>> =A0 =A0This option should be set to '1' to force nested elements to be
>> =A0 =A0represented 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 =3D> 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.

- Grant

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

Re: Hash or array & retrieving hash value

am 16.06.2011 05:06:55 von Rob Dixon

On 16/06/2011 02:15, 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.
>
> 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']

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.

Rob

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

Re: Hash or array & retrieving hash value

am 16.06.2011 19:03:30 von grant

>>>> 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. =A0I 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. =A0Can 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
>>>
>>>> =A0 =A0* =A0 check out "ForceArray" because you'll almost certainly wa=
nt to
>>>> turn
>>>> =A0 =A0 =A0 =A0it on
>>>
>>> and
>>>
>>>> =A0ForceArray =3D> =A01 *# in - important*
>>>> =A0 =A0This option should be set to '1' to force nested elements to be
>>>> =A0 =A0represented as arrays even when there is only one.
>>
>> Thanks Rob. =A0I think ForceArray is what I need but I can't come up
>> with the right setting. =A0If I set 'ForceArray =3D> =A01', everything s=
eems
>> to be forced into an array but I don't want to loop with foreach for
>> every value I want to retrieve. =A0I'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
>
> =A0ForceArray =3D> ['Image']

I tried that and it seems to behave the same as 'ForceArray =3D> 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 =3D>
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Hash or array & retrieving hash value

am 16.06.2011 19:13:07 von Uri Guttman

>>>>> "G" == Grant writes:

>> =A0ForceArray =3D> ['Image']

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


that ForceArray should work as i have used it just like that. it will
only force arrays of the keys you pass to it. it is very useful for xml
which can be a single value or a list and the xml itself will be
different causing you to check the ref when you process it.

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

no, that is overkill. it may work but you are wasting cpu and ram doing it.

show us how you are making the actual call to XMLin and its args. i
suspect you are not using forcearray correctly. it takes an anon array
reference and i bet you passed it a single string (which would act like
a 1 - true value).

uri

--=20
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

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

Re: Hash or array & retrieving hash value

am 16.06.2011 20:20:50 von Rob Dixon

On 16/06/2011 18:03, 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.
>>>
>>> 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?

Hi Grant

I am surprised that ForceArray => ['Image'] didn't work as I expected.

You can apply the option throughout as you have done, but it makes
accessing the resultant data structure very untidy as you have found.
(By the way, all arrows between braces/brackets may be omitted, so you
could write $parsed_response->{Label}[0]{Image}.)

I never recommend XML::Simple as a starting point, as I feel it is the
implementation of the module rather than its use that is 'simple'. There
are several alternatives, but without seeing your data and application
requirement it is difficult to recommend sticking with XML::Simple or
using something different.

Please show your XML and Perl code.

Rob

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