A couple of hash questions

Discussion in 'Perl Misc' started by Dave Saville, Sep 28, 2003.

  1. Dave Saville

    Dave Saville Guest

    If I have a complicated structure, say a hash of hashes/arrays etc and
    I want to save it for later I can use store().

    If I only have a simple hash, just plain name/value pairs, then is it
    more efficient to use store() or a plain text file? Taking into account
    reading the thing back in.

    If the latter, is there a better way to read it back in, assuming space
    separated one pair/line, than:

    ($name, $value) = split;
    $hash{$name} = $value;

    TIA

    Regards

    Dave Saville

    NB switch saville for nospam in address
    Dave Saville, Sep 28, 2003
    #1
    1. Advertising

  2. "Dave Saville" <> wrote in message
    news:...
    > If I have a complicated structure, say a hash of hashes/arrays etc and
    > I want to save it for later I can use store().
    >
    > If I only have a simple hash, just plain name/value pairs, then is it
    > more efficient to use store() or a plain text file? Taking into account
    > reading the thing back in.
    >


    The answer probably depends on your definition of "efficient": fastest?
    smallest memory footprint? It also probably depends on the size of the hash
    you're manipulating.

    If it's a question of speed, the answer is probably best determined thru
    benchmarking. (perldoc Benchmark)

    > If the latter, is there a better way to read it back in, assuming space
    > separated one pair/line, than:
    >
    > ($name, $value) = split;
    > $hash{$name} = $value;
    >

    Probably as good as you will get.
    James E Keenan, Sep 28, 2003
    #2
    1. Advertising

  3. Dave Saville wrote:
    > If I only have a simple hash, just plain name/value pairs, then is
    > it more efficient to use store() or a plain text file? Taking into
    > account reading the thing back in.


    Your direct question was commented on by James.

    Have you considered using DBM files?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 28, 2003
    #3
  4. Dave Saville

    Dave Saville Guest

    On Sun, 28 Sep 2003 20:29:52 +0200, Gunnar Hjalmarsson wrote:

    >Dave Saville wrote:
    >> If I only have a simple hash, just plain name/value pairs, then is
    >> it more efficient to use store() or a plain text file? Taking into
    >> account reading the thing back in.

    >
    >Your direct question was commented on by James.
    >
    >Have you considered using DBM files?


    For what I am trying to do that would be overkill - Thanks for the
    suggestion.

    Regards

    Dave Saville

    NB switch saville for nospam in address
    Dave Saville, Sep 28, 2003
    #4
  5. Dave Saville

    Grendel Guest

    In article <>,
    Dave Saville wrote:

    > If the latter, is there a better way to read it back in, assuming space
    > separated one pair/line, than:
    >
    > ($name, $value) = split;
    > $hash{$name} = $value;


    That'll work, and if you're doing something that doesn't warrant a DBM
    file or such, I'll guess that the speed of how you're reading in a flat
    file won't matter either.

    You could do something with map, on the other hand, if you've already
    got all the delimited data in an array somehow and you know there won't
    by any empty values..

    %hash = map { split(':',$_) } @data;



    --
    | grendel at durge dot org | web technologist | london, uk
    | "It's people like you what cause unrest"
    Grendel, Sep 28, 2003
    #5
  6. Dave Saville

    Dick Penny Guest

    "Dave Saville" <> wrote in message
    news:...
    > If I have a complicated structure, say a hash of hashes/arrays etc and
    > I want to save it for later I can use store().
    >

    I'm no guru, but I believe I saw in this posting (2-6 months ago) the same
    type of Q, and it was answered by one of the gurus to the effect of use
    data:dumper (or is it dump:data) and print the thing to a file,
    sort of like print FILE, data:dump($hash ref); [or whatever the darn syntax
    should be]

    You can see my Perl is far from perfect, but the idea seemed good. I never
    tried it, nor do I remember how to get it back.

    HTH
    Dick Pennt
    Dick Penny, Sep 29, 2003
    #6
  7. Dave Saville

    Vlad Tepes Guest

    On the CCLXXII'nd day of the MMIII'rd year, Dave Saville spoketh:

    > If I have a complicated structure, say a hash of hashes/arrays etc and
    > I want to save it for later I can use store().


    Yes. There are a number of modules you could use for this, including
    Storable and Data::Dumper. Attribute::persistent is a cool one you
    might want to check. You could store it in a database with MLDBM.

    > If I only have a simple hash, just plain name/value pairs, then is it
    > more efficient to use store() or a plain text file? Taking into account
    > reading the thing back in.


    The difference in speed probably is insignificant unless you have
    a lot of data. But don't take my word for it, check for yourself. If
    you wonder about performance of different methods, it's time to
    learn how to use the Benchmark module. The pods contains examples,
    and the module is easy to use.

    > If the latter, is there a better way to read it back in, assuming space
    > separated one pair/line, than:
    >
    > ($name, $value) = split;
    > $hash{$name} = $value;


    split() is fine. Another way is using a capturing regex. If the data
    is in fixed-width columns you could use unpack() or substr().

    --
    (,_ ,_, _,)
    /|\`\._( )_./'/|\
    · · \/ L /\ D · ·
    /__|.-'`-\_/-`'-.|__\
    `·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.. ` " `
    Vlad Tepes, Sep 29, 2003
    #7
  8. Dave Saville wrote:
    > On Sun, 28 Sep 2003 20:29:52 +0200, Gunnar Hjalmarsson wrote:
    >> Dave Saville wrote:
    >>> If I only have a simple hash, just plain name/value pairs, then
    >>> is it more efficient to use store() or a plain text file?
    >>> Taking into account reading the thing back in.

    >>
    >> Have you considered using DBM files?

    >
    > For what I am trying to do that would be overkill


    That made me curious, Dave.

    I have never used Storable, but I'm storing a few small hashes in SDBM
    files. I have never benchmarked, but it seems to be more durable than
    plain text files, and the minimum size of an SDBM database is just 1 kiB.

    What is it that makes you say it would be overkill?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 29, 2003
    #8
  9. Dave Saville

    Dave Saville Guest

    On 28 Sep 2003 20:42:28 GMT, Grendel wrote:

    >In article <>,
    >Dave Saville wrote:
    >
    >> If the latter, is there a better way to read it back in, assuming space
    >> separated one pair/line, than:
    >>
    >> ($name, $value) = split;
    >> $hash{$name} = $value;

    >
    >That'll work, and if you're doing something that doesn't warrant a DBM
    >file or such, I'll guess that the speed of how you're reading in a flat
    >file won't matter either.
    >
    >You could do something with map, on the other hand, if you've already
    >got all the delimited data in an array somehow and you know there won't
    >by any empty values..
    >
    >%hash = map { split(':',$_) } @data;


    Now that looks interesting - slurp the lot into an array and build the
    hash out of the array. As long as I've enough memory :) But I *think*
    the file will only grow to 40K ish. Each pair is only 20 characters.

    Time for playing I think - Thanks to those who pointed out benchmark.

    Regards

    Dave Saville

    NB switch saville for nospam in address
    Dave Saville, Sep 29, 2003
    #9
  10. Dave Saville

    Dave Saville Guest

    On Mon, 29 Sep 2003 06:57:18 +0200, Gunnar Hjalmarsson wrote:

    >> For what I am trying to do that would be overkill

    >
    >That made me curious, Dave.
    >
    >I have never used Storable, but I'm storing a few small hashes in SDBM
    >files. I have never benchmarked, but it seems to be more durable than
    >plain text files, and the minimum size of an SDBM database is just 1 kiB.
    >
    >What is it that makes you say it would be overkill?


    I am only talking a couple of thousand pairs - and I don't have a DB
    installed - at least not on this box and in any case I might be
    releasing the code to a few friends and I can be sure they won't - a
    plain file or a perl internal type file would be portable code.

    Regards

    Dave Saville

    NB switch saville for nospam in address
    Dave Saville, Sep 29, 2003
    #10
  11. Dave Saville wrote:
    > On Mon, 29 Sep 2003 06:57:18 +0200, Gunnar Hjalmarsson wrote:
    >> I have never used Storable, but I'm storing a few small hashes in
    >> SDBM files. I have never benchmarked, but it seems to be more
    >> durable than plain text files, and the minimum size of an SDBM
    >> database is just 1 kiB.
    >>
    >> What is it that makes you say it would be overkill?

    >
    > I am only talking a couple of thousand pairs - and I don't have a
    > DB installed - at least not on this box and in any case I might be
    > releasing the code to a few friends and I can be sure they won't -
    > a plain file or a perl internal type file would be portable code.


    Simple DBM files (SDBM) are portable, at least between *nix and
    Windows platforms. SDBM_File.pm, which gives you tied access to such
    files, comes with Perl, and does not require any separate database
    program.

    I'm not saying it's better, but I still believe it would be an
    alternative to you.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 29, 2003
    #11
  12. Dave Saville

    Dave Saville Guest

    On Mon, 29 Sep 2003 12:10:09 +0200, Gunnar Hjalmarsson wrote:

    >Simple DBM files (SDBM) are portable, at least between *nix and
    >Windows platforms. SDBM_File.pm, which gives you tied access to such
    >files, comes with Perl, and does not require any separate database
    >program.
    >
    >I'm not saying it's better, but I still believe it would be an
    >alternative to you.


    OK I will look into it. Thanks.

    Regards

    Dave Saville

    NB switch saville for nospam in address
    Dave Saville, Sep 29, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne  Wengert

    A Couple of Questions

    Wayne Wengert, Jan 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    325
    =?Utf-8?B?aSBkb250IGtub3cgbXVjaCBhYm91dCB2cy4uLmI=
    Jan 22, 2004
  2. Martin

    A Couple Of Questions

    Martin, Mar 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    346
    Mark Jerde
    Mar 6, 2004
  3. Ali Syed
    Replies:
    3
    Views:
    552
    Mark McIntyre
    Oct 13, 2004
  4. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
Loading...

Share This Page