Re: Storing hash with key & value in a file
by Discipulus (Canon) on Dec 16, 2014 at 09:30 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Storing hash with key & value in a file
by Discipulus (Canon) on Dec 16, 2014 at 10:12 UTC
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: Storing hash with key & value in a file
by tune (Curate) on Dec 16, 2014 at 10:58 UTC
|
Some people prefer JSON to do this.
| [reply] [Watch: Dir/Any] |
|
I am more in favour of YAML.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [Watch: Dir/Any] |
Re: Storing hash with key & value in a file
by vinoth.ree (Monsignor) on Dec 16, 2014 at 10:04 UTC
|
use Storable;
store \%hash, 'filename';
$hashref = retrieve('filename');
This code stores the %hash into filename, the last line retrieves the stored hash from the file. if you wanted a hash instead of a hash reference, wrap the "retrieve" call in side %{}
my %original_hash = %{retrieve('filename')};
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Storing hash with key & value in a file
by CountZero (Bishop) on Dec 16, 2014 at 13:19 UTC
|
Dumping a hash-variable to a file is easy and some suggestions have already been made.However, you want to dump the content of the hash and add some comments to each value. That is not so simple and before we can even think how to approach this, you will need to explain where those comments are found. Are they in another hash or array or file or database or are they perhaps "calculated" on the fly?
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [Watch: Dir/Any] |
Re: Storing hash with key & value in a file
by sundialsvc4 (Abbot) on Dec 16, 2014 at 13:19 UTC
|
FYI: A few years ago, I had “unexplained problems” with Storable, in which it successfully stored nearly all, but not all, of my records. I therefore quickly switched to JSON, and during the course of the project found it quite advantageous to be able to easily look at what a particular record contained. This was a number of years ago, so it might have been fixed now, and in any case I never did piece together what the bug in Storable might have been. (Nothing to do with Unicode or any of the other “usual suspects ...” It just stored it but then couldn’t retrieve what it had stored. I haven’t used the module since.)
| [reply] [Watch: Dir/Any] |