Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default Big hash question

    I have a very large hash inside a script.
    The $values of the $keys can be very large.

    When the script runs, it takes a long time to load the hash.
    And this eats up memory...really fast!
    At least I'm assuming that's what it's doing.

    My question is, is there a way to stop this?
    How can I keep the whole hash from loading at startup?

    while ( ($key, $value) = each %hash) {

    # This is what I'm using for the hash

    }

    Thanks.


    S. Heiling Guest

  2. Similar Questions and Discussions

    1. Simple Hash Question / NEVER MIND
      DOH ..... DOH ..... NEVER MIND, user error ( <- gun to head) DOH ..... DOH ..... DOH ..... ...
    2. Simple Hash Question
      Hi .... I have a hash defined as my %flag3 = ( A => "DBSpace backup thread", B => "Begin work", C => "Commiting/committed", H => "Heuristic...
    3. [PHP-DEV] hash table question
      For my upcoming improvement of interbase.c, which features asynchronous handling of events posted in the database, I need to maintain a per-link...
    4. Another reference question (hash of hash references)
      beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes......
    5. hash generation question
      Stephan wrote: How's this related to the modules list? Anyhow, what about this: my $hashref = \%hash; $hashref = $hashref->{$_} foreach...
  3. #2

    Default Re: Big hash question

    "Tie your hash to a file, or use a database rather than a hash."

    Well this is a rather odd experiment I'm tring.
    And I'm trying to work it from a single script.

    "Don't load the whole hash at startup."

    How can I do that? Just simply being inside the script makes it load up
    memory as far as I can tell. A long - long wait depending on size. I'd like
    it to not load. I've tried to put it in a sub routine as well.

    "If you want us to help you not load the whole hash at start-up,
    you should show us the code that loads the hash, not the code that
    later iterates over the hash."

    I'm not loading it. It's just there.
    And the script ends by running the hash.
    This is what it looks like:

    #####################################
    %hash = (

    key0 => "very long value.......",
    key1 => "valy long value.......",

    );
    while ( ($key, $value) = each %hash) {

    # The keys are file names or directory paths.
    # The values are the file data unpacked into hex.."H"

    }
    #####################################

    I've tried putting the hash after the while statement but still, a long
    wait.
    This is quite unconventional, I know, but is there a way?

    Thanks.




    S. Heiling Guest

  4. #3

    Default Re: Big hash question

    S. Heiling <newsreadermail@charter.net> wrote:
    > And I'm trying to work it from a single script.

    Perhaps you could just put the data in a __DATA__ section.

    > "Don't load the whole hash at startup."
    >
    > How can I do that?

    The post you quoted had code that does that.

    I do not see that you even need a hash at all...

    > %hash = (
    >
    > key0 => "very long value.......",
    > key1 => "valy long value.......",
    >
    > );

    Remove that code that loads the hash, put the data in __DATA__ instead.

    > while ( ($key, $value) = each %hash) {
    >
    > # The keys are file names or directory paths.
    > # The values are the file data unpacked into hex.."H"
    >
    > }
    > I've tried putting the hash after the while statement but still, a long
    > wait.

    Perl compiles your entire program, so where it is located in the
    source code will not matter.

    > is there a way?
    --------------------------------------
    # untested
    while ( <DATA> ) {
    chomp;
    my($key, $value) = split / => /;

    # do stuff with only 1 pair at a time in memory
    }

    __DATA__
    key0 => very long value.......
    key1 => valy long value.......
    --------------------------------------


    See the "Scalar value constructors" section in perldata.pod
    for info on the DATA filehandle and the __DATA__ token.


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #4

    Default Re: Big hash question

    "Perhaps you could just put the data in a __DATA__ section."

    Thanks. I'm working on using __DATA__, and it stops anything after that from
    "compiling", but I'm having some trouble getting anything out of my hash.


    "Tad McClellan" <tadmc@augustmail.com> wrote in message
    news:slrnbh0ga9.anm.tadmc@magna.augustmail.com...
    > S. Heiling <newsreadermail@charter.net> wrote:
    >
    > > And I'm trying to work it from a single script.
    >
    >
    > Perhaps you could just put the data in a __DATA__ section.
    >
    >
    > > "Don't load the whole hash at startup."
    > >
    > > How can I do that?
    >
    >
    > The post you quoted had code that does that.
    >
    > I do not see that you even need a hash at all...
    >
    >
    > > %hash = (
    > >
    > > key0 => "very long value.......",
    > > key1 => "valy long value.......",
    > >
    > > );
    >
    >
    > Remove that code that loads the hash, put the data in __DATA__ instead.
    >
    >
    > > while ( ($key, $value) = each %hash) {
    > >
    > > # The keys are file names or directory paths.
    > > # The values are the file data unpacked into hex.."H"
    > >
    > > }
    >
    >
    > > I've tried putting the hash after the while statement but still, a long
    > > wait.
    >
    >
    > Perl compiles your entire program, so where it is located in the
    > source code will not matter.
    >
    >
    > > is there a way?
    >
    > --------------------------------------
    > # untested
    > while ( <DATA> ) {
    > chomp;
    > my($key, $value) = split / => /;
    >
    > # do stuff with only 1 pair at a time in memory
    > }
    >
    > __DATA__
    > key0 => very long value.......
    > key1 => valy long value.......
    > --------------------------------------
    >
    >
    > See the "Scalar value constructors" section in perldata.pod
    > for info on the DATA filehandle and the __DATA__ token.
    >
    >
    > --
    > Tad McClellan SGML consulting
    > [email]tadmc@augustmail.com[/email] Perl programming
    > Fort Worth, Texas

    S. Heiling Guest

  6. #5

    Default Re: Big hash question

    S. Heiling <newsreadermail@charter.net> wrote:
    > "Perhaps you could just put the data in a __DATA__ section."

    Who said that?

    Please provide an attribution when you quote someone.

    Please mark quoted text following Usenet convention, so you don't
    confuse people's newsreaders.

    Have you seen the Posting Guidelines that are posted here frequently?

    > Thanks.

    If you mean that, then please learn to compose followups properly.

    Soon.



    [ snip 75 lines of TOFU ]


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  7. #6

    Default Re: Big hash question

    "S. Heiling" <newsreadermail@charter.net> wrote in message news:<vgu39at0610747@corp.supernews.com>...
    > I have a very large hash inside a script.
    > The $values of the $keys can be very large.
    >
    > When the script runs, it takes a long time to load the hash.
    > And this eats up memory...really fast!
    > At least I'm assuming that's what it's doing.
    >
    > My question is, is there a way to stop this?
    > How can I keep the whole hash from loading at startup?
    >
    > while ( ($key, $value) = each %hash) {
    >
    > # This is what I'm using for the hash
    >
    > }
    >
    foreach $key (keys %hash) {

    $hash{$key} = "blabla";
    # or:
    print "$hash{$key}\n";

    }


    Rgds

    Ingo ;-))

    > Thanks.
    Ingo Fellner Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139