Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Storing hash with key & value in a file

by kpsinha (Initiate)
on Dec 16, 2014 at 09:25 UTC ( #1110468=perlquestion: print w/replies, xml ) Need Help??

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

Hi, i want to store key as well as value of a hash with some modification like key1 value1 **some text** key2 value2 **some new text** unable to do ... Please help
  • Comment on Storing hash with key & value in a file

Replies are listed 'Best First'.
Re: Storing hash with key & value in a file
by Discipulus (Abbot) on Dec 16, 2014 at 09:30 UTC
    hello kpsinha

    what have you tried? have you explored core modules or alternatives to store Perl data structures on filesystems?

    Better the question better the answer, possibly.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Storing hash with key & value in a file
by Discipulus (Abbot) on Dec 16, 2014 at 10:12 UTC
    As said in the chat, if you need to modify the text dumped to the file adding some given **text**, probably you need to do it by hand: open a file to write to, cycle on (sorted?) keys of such hash and print what you had to print to file.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
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.

    --
    tune

      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
Re: Storing hash with key & value in a file
by vinoth.ree (Monsignor) on Dec 16, 2014 at 10:04 UTC

    Hi,

    Check Storable module.

    Example:
    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')};

    All is well
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
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.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2020-08-10 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?