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

Morning Monks,
I got a hash that I want to save to a file, save the values for all the keys actually.
Here is the hash:

my %stats=('sent',0, 'recv',1, 'masters',2, 'errors',3);

I want to save all the values in a single file in the following format:

0,1,2,3


How would I do this? I allready know how to use join().
Thank You ^jasper <jasper@wintermarket.org>

Replies are listed 'Best First'.
Re: Saving Hash Values to file
by strat (Canon) on May 07, 2002 at 09:18 UTC
    For such tasks, I prefer using Storable. If you want to dump your hash(-references) in a human readable format, I like to use Data::Dump or Data::Dumper (if you don't use circular references).

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: Saving Hash Values to file
by gav^ (Curate) on May 07, 2002 at 00:56 UTC
    This should do the trick:
    open OUT, 'file' or die $!; print OUT join(',', sort { $a <=> $b } values %stats), "\n"; close OUT;

    gav^

      he probably want to keep the order of the keys, not sort in ascending order... if he reads the file later, he won't know which number represents which value.
      compare the 2 lines of output :
      use strict; my %stats=('sent',0, 'recv',4, 'masters',2, 'errors',2 ); my @saveorder = qw (sent recv masters errors); print join (",", @stats{@saveorder}), "\n", join(',', sort { $a <=> $b } values %stats), "\n";
      The first one will print out in the order specified in @saveorder, so you know which item comes where in your list. The second is just sorted, which could make for unpredictability later on in life...
      See also storable, but that's probably overkill here.

      Update : Almost forgot - jaspersan, take a look in perldata for array slices or hash slices.

        If you want to store and retreve a hash then why not use dbmopen?

        Storable is worth discovering too?

Re: Saving Hash Values to file
by jbert (Priest) on May 07, 2002 at 11:47 UTC
    If you want to save the hash values only, you need to define a mapping between the order of the values in the file and the keys. If your needs are any more complex than this (and they might get so in the future) you might want to consider using the modules people have mentioned (e.g. Data::Dumper and Storable).
    Some people would do this with code only. You can have 'serialise()' and 'deserialise()' functions which contain the order embedded as a sequence of print or assignment statements.
    I would also have two functions, but store the order in a shared array (This serves two purposes... "Data is data, not code" and "once and once only"). I would probably also do this in an object, but for the purposes here, I won't:
    #!/usr/local/bin/perl -w use strict; my @hash_order = qw( sent recv masters errors ); my $hash_sep = ","; # Return ordered hash values, in one line. Hash passed in by reference sub join_my_hash { my $hr = shift; # The 'map' is just making sure that undefined values # are handled the same as "". return join( $hash_sep, map { defined $_ ? $_ : "" } @$hr{@hash_order} ); } # Return hash (or ref to hash in scalar context) sub split_my_hash { my $line = shift; my %hash; # We don't have a check for too few seperators # We should probably map undefined values here too. @hash{@hash_order} = split( $hash_sep, $line ); return wantarray() ? %hash : \%hash; } # Test it out. my %h = ( sent => 1, recv => 2, masters => 0, errors => 10 , ); # A join my $line = join_my_hash( \%h ); print "join_my_hash: $line\n"; # And split back to a different array my %hn = split_my_hash( $line ); foreach my $k ( keys %hn ) { print "$k: $hn{$k}\n"; }
      Thanks for all the help monks. I'll look into those modules too. :)
      ^jasper <jasper@wintermarket.org>
A reply falls below the community's threshold of quality. You may see it by logging in.