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

Kind Monks: Unfortunately, this question only arose due to my hardware being a 486, so I guess the proper answer would be to buy a new computer, but i digress...
I have a big hash of lists, like so:
$cords{1} = [a,b]; $cords{2} = [c,d]; ... $cords{9999} = [w,x]; $cords{10000} = [y,z];
The list items a..z are calculated by a complex algorythm, which unfortunately takes 6 minutes to compute all 10000 hash elements. Not being the best programmer in the world, I need to test my script a lot, which gets pretty tedious waiting for the hash to be populated, so...
Can I save it to disk? The nums it calculates are always the same, so thats no problem. I need to maintain the structure obviously, is there some way of doing this?

Replies are listed 'Best First'.
Re: Saving a %hash
by demerphq (Chancellor) on Oct 20, 2001 at 18:33 UTC
    As per Tillys comments, using Data::Dumper is one of the ways to go
    use Data::Dumper; use Carp; use strict; use warnings; sub write_to_file { my ($file,$data,$small)=@_; open my $OUT,">$file" or croak "Error with $file:$!"; my $dumper=Data::Dumper->new([$data],["my_dump"]); $dumper->Indent($small ? 0 : 2); print $OUT $dumper->Dump; close $OUT; return $dumper->Dump; } sub read_from_file { my $file=shift; local $/; open my $IN,"$file" or croak "Error with $file:$!"; my $data=<$IN>; close $IN; my $my_dump; eval $data; croak "from $file I get error $@" if $@; return $my_dump; } my $test={key=>[qw(this is a list)], subhash=>{qw(so is this one but it must have pairs inside)}} +; write_to_file("test_dumper.dmp",$test); my $var=read_from_file("test_dumper.dmp"); print Dumper($test,$var);
    Of course you could use Storable, or Data::Denter or a variety of other methods depending on your needs and space/time/memory requirements. But the type of code above can be a simple way to do configuration files and other simple storage requirements.
    Note that there can be security concerns doing this kind of thing with eval.

    HTH

    Yves
    --
    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

    Update I see that I must learn to type faster, or check for replys posted since I started to reply. Nice one Amoe. Or post shorter comments. :-)

Re: Saving a %hash
by Amoe (Friar) on Oct 20, 2001 at 18:21 UTC
    use Data::Dumper; To dump:
    open DUMP, ">$dump_path" or die "Couldn't open file to dump: $!"; print DUMP Data::Dumper->Dump([\%coords], ['coords']); close DUMP; # close the city dump
    To load:
    open LOAD, "<$dump_path" or die "Couldn't open file to load: $!"; local $/; $/ = undef; my $struct = <LOAD>; close LOAD; eval { $struct }; # then access %coords as usual


    --
    my one true love
Re: Saving a %hash
by Fletch (Bishop) on Oct 20, 2001 at 18:39 UTC

    You might want to look into using Memoize rather than rolling something yourself with MLDBM or Storable. It will cache return values as they're created, and it will return already computed values rather than redoing the work. It also is capable of storing values to disk.

Re: Saving a %hash
by DrManhattan (Chaplain) on Oct 20, 2001 at 22:55 UTC
    Another alternative is to tie() your hash to a berkely db with DB_File.
    use strict; use DB_File; my %cords; tie %cords, "DB_File", "file_name"; if (!defined($cords{10000})) { # generate %cords hash again ... }
    Update: The Silent Monk crazyinsomniac reminds me that DB_File doesn't support complex data structures. You can't store something like an array ref in a Berkeley DB.

    -Matt

Re: Saving a %hash
by Hofmator (Curate) on Oct 22, 2001 at 15:16 UTC

    Not relating to your problem - but may I ask why are you using a hash for what looks like a perfect place for an array:

    #instead of $cords{1} = ... $cords[1] = [a,b];

    -- Hofmator

      Hey, sometimes you wake up, and your just not in the mood for an array.
      Actually, later on the script uses a lookup table, and I only know how to call a sub who's name is in a hash, like so:

      &{$cords{$x}};

      Im sure you can do it w/ an array, but brain was disengaged during planning, AS PER USUAL ;-)

      SMiTZ
Re: Saving a %hash
by smitz (Chaplain) on Oct 21, 2001 at 19:21 UTC
    Damn, there really is more than one way to do it...
    ++ to george sherston for anticipating my update and answering an un-posted question, nice!

    Best answer award goes to tilly for recommending Storable, have a look at it if youv'e never come across it before.

    Again, many thanks!

    SMiTZ
A reply falls below the community's threshold of quality. You may see it by logging in.