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

Hi monks, I am struck up in a problem. I have a hash like this

my %hash = { "manager.1.address" => "127.0.0.1" "manager.1.port => "162" "manager.1.version" => "2" manager.2.address" => "127.3.2.1" "manager.2.port => "132" "manager.2.version" => "1" } <p>
Now i want it to write to a file in this form

manager.1.address=127.0.0.1 manager.1.port=162 manager.1.version=2

and so on like this... How should i write this hash to a file. }

Edit: g0n - Added code tags

Replies are listed 'Best First'.
Re: how to assign a hash to a file
by svenXY (Deacon) on Sep 20, 2005 at 12:59 UTC
    Hi,
    what you seem to want here is not assign the hash to a file (that would be a tie), but to just write out as key value pairs:
    #!/usr/bin/perl use strict; use warnings; my %hash = ( "manager.1.address" => "127.0.0.1", "manager.1.port" => "162", "manager.1.version" => "2", "manager.2.address" => "127.3.2.1", "manager.2.port" => "132", "manager.2.version" => "1" ); open(OUT, ">$outfile") or die "Could not open file for writing: " . $! +; foreach (keys %hash) { print OUT "$_=$hash{$_}\n"; } close OUT;
    prints (aka writes)
    manager.1.port=162 manager.1.version=2 manager.2.address=127.3.2.1 manager.2.port=132 manager.2.version=1 manager.1.address=127.0.0.1
    to the file
    Regards,
    svenXY
Re: how to assign a hash to a file
by ikegami (Patriarch) on Sep 20, 2005 at 14:07 UTC

    Note that
    %hash = { ... };
    should be
    %hash = ( ... );
    The curlies create an anonymous hash and return a reference to that hash. However, when you assign to a hash, you want to assign a list of key/values, not a reference to another hash. svenXY silently fixed this in his reply, but I thought it was worth mentioning.

Re: how to assign a hash to a file
by chester (Hermit) on Sep 20, 2005 at 13:38 UTC
    Config::IniHash? There are a million and a half config modules, serialization modules, Data::Dumper and friends, etc. etc. CPAN is your friend.
Re: how to assign a hash to a file
by blazar (Canon) on Sep 20, 2005 at 13:01 UTC
    How should i write this hash to a file.
    Incidentally: s/\.$/?/;

    Whatever: how 'bout print, keys and $hash{$key} (or each)? Oh, and pardon me if it seems I'm behaving with an attitude in your respects, but I really think perlintro is in order...

      Oh, and pardon me if it seems I'm behaving with an attitude in your respects, but I really think perlintro is in order...
      Unless this course of action has been suggested for this person before, I'd grant leniency if it were me. When you're just starting out with somthing, it's difficult to know where to begin looking sometimes.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        I have to agree with you - there's so much FM to R, it's hard to know where to begin. Even with Super Search, it's often hard to know just what keywords to use to find the stuff you're looking for.

Re: how to assign a hash to a file
by Anonymous Monk on Sep 20, 2005 at 17:53 UTC