Click here to get back home

reading array data back into perl with YAML

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
reading array data back into perl with YAML bijanmossadeghi@yahoo.com 12-01-2005
Posted by bijanmossadeghi@yahoo.com on December 1, 2005, 2:22 pm
Please log in for more thread options


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(0x8a3dfcc)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.



Posted by ingy on December 2, 2005, 11:38 am
Please log in for more thread options



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(0x8a3dfcc)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.



Posted by Sisyphus on December 3, 2005, 8:53 am
Please log in for more thread options



.
..
>
> @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




Similar ThreadsPosted
Output binary data in base64 in YAML January 14, 2005, 7:20 pm
Reading Data::Dumper output February 8, 2008, 12:11 pm
How to convert ArrayOfstring in SOAP into an array of PERL? July 24, 2007, 3:25 pm
Reading listbox/combobox/textbox using perl? May 16, 2007, 6:01 pm
How to tie with YAML but also have flock support? December 23, 2007, 9:11 am
Tie::YAML doesn't write to tmp file first? December 23, 2007, 9:34 am
FindBin cannot chdir back problem July 12, 2005, 5:23 pm
double back-slash in Parse::RecDescent grammars? October 9, 2006, 5:43 pm
CGI::StringDB Embedding perl data structures in an HTML post. July 10, 2005, 9:13 pm
ANNOUNCE: Set::Array V 0.13 April 9, 2006, 12:25 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap