reading array data back into perl with YAML
am 01.12.2005 23:22:20 von bijanmossadeghi
I have a Perl hash:
%dictionary = (
gema => [1],
gemaadm => [1],
admin => [1],
Prague => [1],
server => [1]
);
and an array:
@data = (
[gemaadm, abc123, "gema admin for Prague server"],
[csvadm, abc123, "csv server in us"]
);
i loaded these data structures into files with YAML:
YAML::DumpFile("dictionary.dat",\%dictionary);
YAML::DumpFile("data.dat",\@data);
when i read the data back into Perl and print them, the hahs keys
comeout ok, but the values come out like ARRAY(0x8a3dfcc)
%dictionary = %{ YAML::LoadFile("dictionary.dat")};
print %dictionary;
adminARRAY(0x8a3df84)gemaARRAY(0x8a3dfb4)gemaadmARRAY(0x8a3d fcc)serverARRAY(0x8a3dfe4)PragueARRAY(0x8a40200)
same thing with the array
@data = YAML::LoadFile("data.dat");
pritn @data;
ARRAY(0x8b92594)
question is, how do i get the hash values back into being arrays, and
how do i get the array to come back into perl as an array?
thanks.
Re: reading array data back into perl with YAML
am 02.12.2005 20:38:15 von Ingy
bijanmossadeghi@yahoo.com wrote:
> I have a Perl hash:
>
> %dictionary = (
> gema => [1],
> gemaadm => [1],
> admin => [1],
> Prague => [1],
> server => [1]
> );
>
> and an array:
>
> @data = (
> [gemaadm, abc123, "gema admin for Prague server"],
> [csvadm, abc123, "csv server in us"]
> );
>
> i loaded these data structures into files with YAML:
> YAML::DumpFile("dictionary.dat",\%dictionary);
> YAML::DumpFile("data.dat",\@data);
>
> when i read the data back into Perl and print them, the hahs keys
> comeout ok, but the values come out like ARRAY(0x8a3dfcc)
>
> %dictionary = %{ YAML::LoadFile("dictionary.dat")};
> print %dictionary;
>
> adminARRAY(0x8a3df84)gemaARRAY(0x8a3dfb4)gemaadmARRAY(0x8a3d fcc)serverARRAY(0x8a3dfe4)PragueARRAY(0x8a40200)
>
>
> same thing with the array
>
> @data = YAML::LoadFile("data.dat");
> pritn @data;
>
> ARRAY(0x8b92594)
This has nothing to do with YAML. Try this Perl one liner:
perl -le 'print [1, 2, 3]'
This is just how Perl prints references. In other words, your code is
probably working fine, it's your debugging that needs help. :)
Cheers, Ingy
>
>
>
> question is, how do i get the hash values back into being arrays, and
> how do i get the array to come back into perl as an array?
>
> thanks.
Re: reading array data back into perl with YAML
am 02.12.2005 22:53:26 von Sisyphus
..
..
>
> @data = YAML::LoadFile("data.dat");
> pritn @data;
>
> ARRAY(0x8b92594)
>
That's an array reference that YAML::LoadFile() is returning. A more
appropriate way to code it would be:
$ref = YAML::LoadFile("data.dat");
print "@$ref\n";
which should print out the elements (space delimited) of the array to which
$ref refers.
You need to treat the other array references in your post similarly.
Cheers,
Rob