For Programmers: Free Programming Magazines  


Home > Archive > PERL on RISC OS > June 2005 > saving hashes to a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author saving hashes to a file
Colin Foster

2005-06-08, 9:04 pm

Hi All,

Is there a way of saving hashes to a file and then retrieving them from
the file?

Colin.
Richard Proctor

2005-06-08, 9:04 pm

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 Richard@waveney.org http://www.waveney.org
Telecoms Richard@WaveneyConsulting.com http://www.WaveneyConsulting.com
Web services Richard@wavwebs.com http://www.wavwebs.com
Independent Telecomms Specialist, ATM expert, Web Analyst & Services

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com