Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: conf file in Perl syntax

by Ravendark (Acolyte)
on Jun 11, 2009 at 09:20 UTC ( [id://770574]=note: print w/replies, xml ) Need Help??


in reply to Re: conf file in Perl syntax
in thread conf file in Perl syntax

Good, nice I like it. I read that you can have the values in an array with:
@values = $cfg->val('Section', 'Parameter'); # this will be the array +holding the IPs
How can I get the names of the hosts in an array too?

Replies are listed 'Best First'.
Re^3: conf file in Perl syntax
by targetsmart (Curate) on Jun 11, 2009 at 09:36 UTC
    you can tie to a hash, and get to iterate through the keys(variables) and values(actual values) of the hash.
    an example is given in the man page of the config::inifiles itself. it is easy
    use Config::IniFiles; my %ini tie %ini, 'Config::IniFiles', ( -file => "/path/configfile.ini" ); print "We have $ini{Section}{Parameter}." if $ini{Section}{Parameter +}; my @keys = keys %{$ini{$section}} while (($k, $v) = each %{$ini{$section}}) {...} if( exists %{$ini{$section}}, $parameter ) {...}

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      I am sorry but I dont quite get it. Lets say that we have the following ini file:
      [hosts] host_1 = 192.168.1.1 host_2 = 192.168.1.2 host_3 = 192.168.1.3
      I need to have @hosts, containing: host_1, host_2 & host_3. Also @ips containing 192.168.1.1, 192.168.1.2 & 192.168.1.3 Is this possible? Sorry for me being stupid!
        you should have better experimented with yourself!. :)
        use Config::Inifiles; use Data::Dumper; my %ini; tie %ini, 'Config::IniFiles', ( -file => "test.ini" ); print Dumper \%ini; @hosts = keys %{$ini{'hosts'}}; @ips = values %{$ini{'hosts'}}; print Dumper \@hosts; print Dumper \@ips;
        will print
        $VAR1 = { 'hosts' => { 'host_1' => '192.168.1.1', 'host_2' => '192.168.1.2', 'host_3' => '192.168.1.3' } }; $VAR1 = [ 'host_1', 'host_2', 'host_3' ]; $VAR1 = [ '192.168.1.1', '192.168.1.2', '192.168.1.3' ];

        Vivek
        -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2026-02-10 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.