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

save hash to file?

by Allasso (Scribe)
on Nov 14, 2010 at 13:38 UTC ( #871318=perlquestion: print w/replies, xml ) Need Help??
Allasso has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks,

Is there a way to save a hash or array directly to a binary file, eg, to use as an index for a search engine?

thanks, Allasso

Replies are listed 'Best First'.
Re: save hash to file?
by kennethk (Abbot) on Nov 14, 2010 at 16:10 UTC
    The simple answer is use the core module Storable. Example from the documentation:

    use Storable; store \%table, 'file'; $hashref = retrieve('file');
Re: save hash to file?
by tokpela (Chaplain) on Nov 14, 2010 at 19:51 UTC

    Check out DBM::Deep which will allow you to save your hash as a binary file with a very fast lookup.

      thanks, all good suggestions.


Re: save hash to file?
by PeterPeiGuo (Hermit) on Nov 14, 2010 at 18:46 UTC

    A major defect of this idea is that, all data are virtually loaded in memory all the time. Unless the amount of searchable index is limited, I do not consider this as a workable solution.

    Use a DB in this case, and leave the momery management to the DB.

    Peter (Guo) Pei

Re: save hash to file?
by planetscape (Chancellor) on Nov 15, 2010 at 04:45 UTC
Re: save hash to file?
by andreas1234567 (Vicar) on Nov 15, 2010 at 11:34 UTC
    I would use a human-readable format to store my data, e.g. JSON.

    See also this discussion on for a comparison of YAML, JSON, XML, Storable.

    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: save hash to file?
by sundialsvc4 (Abbot) on Nov 15, 2010 at 14:59 UTC

    Strictly speaking, your inquiry is ambiguous.   The word, “hash,” could mean “a Perl data structure,” or it could mean “(say...) an MD5 or SHA1 digest.”

      It's not ambiguous at all - it says "hash or array", the word "array" gives context to the intended meaning of "hash". Perl uses context too, doesn't it ;)

        Not to nit-pick here... :-D ... but if someone were determined to misunderstand the OP, it would still be possible to do so.   ;-D

        The specific thing that is triggering my uncertainty is the comment, “to use as a search key.”   You probably wouldn’t be expecting to use a JSON-string as a search key; but a SHA1 digest, you might.

        It’s kinda like the old saw about a computer program finding five different valid parses to the string, “Time flies like an arrow.”   You could even parse “save hash to a file” in two different ways:   save(hash, to_file) or save(hash(file)).

        Aww, heck ... obviously, it is time for more coffee.

        TMTOWTCY = There's More Than One Way To Confuse Yourself

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://871318]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-01-18 08:13 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (207 votes). Check out past polls.