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

Hi,
I tried a lot to read a file like .ini.
I would like to store the contents in the format of hashof hash.
My file looks like
# This is a configuration file. [TITLE] Name = John Location = USA [HEAD] Name = James Location = Canada [BODY] Name = Mayer Location = UK
I would like to store this information as
%hash1 = (TITLE =>{Name => John, Location => USA, }, HEAD => {Name => James, Location => Canada, }, BODY=> { Name => Mayer, Location => UK }, );
Could you please give the Sample code.
When i was trying to store the Key values into the hash, I am not able to insert that in the proper way.
Thanks & Regards

Replies are listed 'Best First'.
Re: .ini type file to store contents in hash of hash format
by PodMaster (Abbot) on Aug 29, 2004 at 13:09 UTC
    use the CPAN, use Config::IniFiles
    C:\>more configfile.ini # This is a configuration file. [TITLE] Name = John Location = USA [HEAD] Name = James Location = Canada [BODY] Name = Mayer Location = UK C:\>more config.inifiles.pl use Config::IniFiles; use Data::Dumper; tie my %ini, 'Config::IniFiles', ( -file => "configfile.ini" ); die Dumper( \%ini ); C:\>perl config.inifiles.pl $VAR1 = { 'TITLE' => { 'Location' => 'USA', 'Name' => 'John' }, 'BODY' => { 'Location' => 'UK', 'Name' => 'Mayer' }, 'HEAD' => { 'Location' => 'Canada', 'Name' => 'James' } }; C:\>

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: .ini type file to store contents in hash of hash format
by ccn (Vicar) on Aug 29, 2004 at 13:09 UTC
      Hi,
      Thanks for your reply.
      Without using any module, how can i store the data.
      Waiting for your reply
      Thanks

        Okay, to make up for my snide reply a few minutes ago, I decided to whip this up for you. It should get you started as a rough guide for approaching your problem:

        use strict; use Data::Dumper; my %hash; my $section = '__none__'; while (<DATA>) { next if /^#/; chomp; if ( /^\[([^\]]+)\]$/ ) { $section = $1; } elsif ( /^([^=]+?)\s*=\s*(.*)$/ ) { $hash{$section}{$1} = $2; } } die Dumper( \%hash ); __DATA__ # This is a configuration file. Section = none [TITLE] Name = John Location = USA [HEAD] Name = James Location = Canada [BODY] Name = Mayer Location = UK

        Output:

        $VAR1 = { 'TITLE' => { 'Location' => 'USA', 'Name' => 'John' }, 'BODY' => { 'Location' => 'UK', 'Name' => 'Mayer' }, '__none__' => { 'Section' => 'none' }, 'HEAD' => { 'Location' => 'Canada', 'Name' => 'James' } };
        --
        edan

        Hi,
        Thanks for your reply.
        Without showing us your code, how can you expect us to help you.
        Waiting for your reply
        Thanks
        --
        edan

Re: .ini type file to store contents in hash of hash format
by gaal (Parson) on Aug 29, 2004 at 14:10 UTC
    I like Config::Tiny -- it produces data not in the precise manner you're asking for, so it isn't appropriate if your requirement is fixed. But at under 28 lines, it really is tiny!