Re: saving hashes to a file

[email protected] (Richard Proctor) Thu, 19 Jun 2003 16:58:47 +0100 (BST)
Newsgroups perl.riscos
Message-ID <[email protected]>
On Thu 19 Jun, Colin Foster wrote:
> Hi All,
> 
> Is there a way of saving hashes to a file and then retrieving them from
> the file?
> 
> Colin.
> 
There are 1001+ ways but a simple way is:

To save:

foreach (keys %hash) { print FILE "$_=$hash{$_}\n" }

To read:
while (<FILE>) {
  chomp;
  ($k,$v) = split /=/;
  $hash{$k} = $v;
}

I leave the opening and closing of the FILE to the reader.  If the hash
value contains binary data and/or multiple lines then encode/decode
the value in some suitable form, likewise for the key if necessary.

Richard


-- 
Personal     [email protected]            http://www.waveney.org
Telecoms     [email protected]  http://www.WaveneyConsulting.com
Web services [email protected]            http://www.wavwebs.com
Independent Telecomms Specialist, ATM expert, Web Analyst & Services