Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Saving Hash Values to file

by gav^ (Curate)
on May 07, 2002 at 00:56 UTC ( [id://164497]=note: print w/replies, xml ) Need Help??


in reply to Saving Hash Values to file

This should do the trick:
open OUT, 'file' or die $!; print OUT join(',', sort { $a <=> $b } values %stats), "\n"; close OUT;

gav^

Replies are listed 'Best First'.
Re: Re: Saving Hash Values to file
by boo_radley (Parson) on May 07, 2002 at 01:32 UTC
    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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://164497]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found