Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Saving and restoring hash variable data

by as_ndr (Initiate)
on Dec 08, 2005 at 23:37 UTC ( [id://515419]=perlquestion: print w/replies, xml ) Need Help??

as_ndr has asked for the wisdom of the Perl Monks concerning the following question:

I have a complex hash:
$db{worda}[0] = "stringa"; $db{worda}[1] = "stringb"; $db{worda}[2] = "stringc"; $db{wordb}[0] = "stringd"; $db{wordb}[1] = "stringe"; $db{wordc}[0] = "stringf"; $db{wordd}[0] = "stringg"; $db{worde}[1] = "stringh"; $db{worde}[2] = "stringi"; $db{worde}[3] = "stringj";

but 500 times bigger. Is there a way to save it to a file and restore it later in only a few lines. The file doesn't have to be ledgible to the human eye. I just need a way to save and restore the data from the hash quickly and easily. Thanks!

Replies are listed 'Best First'.
Re: Saving and restoring hash variable data
by GrandFather (Saint) on Dec 08, 2005 at 23:39 UTC
Re: Saving and restoring hash variable data
by Siddartha (Curate) on Dec 09, 2005 at 00:36 UTC
    Also have a look at Data::Dumper. Easiest way I have found to dump and restore most data structures. Has the added convenience of being human readable (As long as you understand Perl syntax).
Re: Saving and restoring hash variable data
by TedPride (Priest) on Dec 09, 2005 at 01:54 UTC
    $rd is the record delimiter and $fd the field delimiter. Both must not be found in either keys or strings. I've used \n and \t for simplicity, but special characters or character sequences are probably smarter.
    my $rd = "\n"; my $fd = "\t"; my %db; $db{worda}[0] = "stringa"; $db{worda}[1] = "stringb"; $db{worda}[2] = "stringc"; $db{wordb}[0] = "stringd"; $db{wordb}[1] = "stringe"; $db{wordc}[0] = "stringf"; $db{wordd}[0] = "stringg"; $db{worde}[1] = "stringh"; $db{worde}[2] = "stringi"; $db{worde}[3] = "stringj"; for (keys %db) { print join $fd, $_, @{$db{$_}}; print $rd; }
    And to reverse the generated data:
    my $rd = "\n"; my $fd = "\t"; my (%db, $p, $i); $/ = $rd; while (<DATA>) { chomp; ($_, @_) = split $fd; $db{$_} = [@_]; } for (sort keys %db) { $p = $db{$_}; for ($i = 0; $i <= $#$p; $i++) { print "\$db{$_}[$i] = \"$p->[$i]\";\n"; } } __DATA__ wordc stringf wordd stringg worde stringh stringi stringj worda stringa stringb stringc wordb stringd stringe
Re: Saving and restoring hash variable data
by as_ndr (Initiate) on Dec 09, 2005 at 14:50 UTC
    Excellent! I will try out the suggestions now! Thanks for your help!
Re: Saving and restoring hash variable data
by as_ndr (Initiate) on Dec 09, 2005 at 16:42 UTC
    Solution I am using:
    use XML::Dumper; my $dump = new XML::Dumper; $db{worda}[0] = "stringa"; $db{worda}[1] = "stringb"; $db{worda}[2] = "stringc"; $db{wordb}[0] = "stringd"; $db{wordb}[1] = "stringe"; $db{wordc}[0] = "stringf"; $db{wordd}[0] = "stringg"; $db{worde}[1] = "stringh"; $db{worde}[2] = "stringi"; $db{worde}[3] = "stringj"; my $file = "dump.xml"; $dump->pl2xml( \%db, $file ); my %perl = $dump->xml2pl( $file ); print "New ref: $perl{worde}[3]\n";

    I can't get the end part to work, though. Once the data is back into %perl, how to I access it correctly?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://515419]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found