Forum Moderators: coopster & phranque

Message Too Old, No Replies

Running out of memory due to big list - ?

I think :-/

         

physics

6:07 pm on Jun 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have just written a program that makes an array of hashes of hashes of hashes. It reads all kind of information in from a bunch of files and then later I access the elements of this thing in a convienient way because it's kind of like a little database. Anyways, I end up printing everything out into a very big html table. I noticed that *sometimes* nothing is printed when I say 'print this part of that array...' Thought it was a bug in the code, but then I ran the program on a machine with more memory and *now* the elements that were missing previously are printed, but some 'further on down the line' are not. It seems that the computer is running out of memory and at some point no data really gets added to this monster of a list of lists... What the heck should I do so that I can perform these functions, but not run into this problem of running out of memory (and is that really the problem)?

physics

7:03 pm on Jun 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Someone suggested:

You might want to look into using DB_File -- your hash will use your disk
as a sort of 'virtual memory', plus your hash will be persistent between
calls of your script:

use DB_File;

tie %myhash, "DB_File", $filename or die "Can't open $filename$!\n";

#do stuff to gather data

$myhash{$somekey} = $somevalue;

#do more stuff with your data

untie %myhash;

I like this idea!

physics

8:30 pm on Jun 13, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The saga continues:
I said:
> Hi, I tried to do this, but it didn't really work because first I
> declare an array like this:
> my @day;
>
> Then I did as you said...
>
> Then, I add information using things like:
>
> $day[$ext]{$line[2]}{$line[0]}{pages}=$line[1];
He said:
Oh, yeah, you're doing some pretty complex data structures there. You
will want to use the MLDBM module instead. You'll have to get it from
CPAN. It is uses Data::Dumper to store complex data structures. It works
like this:

use MLDBM 'DB_File';

tie (%myhash, 'MLDBM', [other args]) or die "...";

$myhash{$somekey} = ["some data", "more data"];

Be very careful, though, because you can't do:

$myhash{$somekey}->[0] = "even more data";

This is a limitation of the hash interface (you can't manipulate data via
references on the disk). Instead, use temp variables:

$moredata = $myhash{$somekey};
$moredata->[0] = "even more data";
$myhash{$somekey} = $moredata;

I say:

Cool, but maybe it's time to consider another approach all together!