Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compute Config file 3

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi,

I have the need to compute a configuration file in order to perform certain tasks. The config file has different chapters.
This is - how i started - but I am kind of stock. I thought to read the config file and store all different chapters in separate array - so I can compute them after - as needed.
But how .. ? Any help appretiated.
Cheers - Dietmar

#!/usr/bin/perl
my $count="0";
$conffile="/tmp/config.cfg";
my (@chapter,@nodegroupdef,@customergroup,@linux,@windows);
open(CONF,"<$conffile") or die "Cannot open $conffile\n";
while (<CONF>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
if ( "$_" eq "^[NodegroupDefault" ) {
......
}



The look of the config file:

###Config File
[NodeGroupDefault]
1HOURHC
PRODUCTION
#
#
[CUSTOMER_GROUP]
customer_a=group_a
customer_b=group_b
customer_c=group_c
#
#
[Assignment_Linux]
Linux_a
Linux_b
#
[Assignment_Windows]
Windows_A
Windows_B
 
Why not create it in xml and read it in that way?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Endusers shell be able to modify entries in the different chapters of the config file easy - thats why we thought to use this classic form of input config file
 
This doesn't do any processing of the data in the config file, nor does it do any error checking, but it might give you somewhere to start.
Code:
my %config;
{
	my $chapter;
	while (<CONF>) {
		next if /^\s*$/;	# Blank line
		next if /^\s*#/;	# Comment
		s/^\s*//;			# Remove leading spaces
		s/\s*$//;			# Remove trailing spaces
		if (m/^\[(\w+)]/) {	# Find Chapter names
			$chapter = $1;
		} else {
			push @{$config{$chapter}}, $_;
		}
	}
}
There are probably a number of modules on CPAN that would be helpful. Take a look at Config::IniFiles or Config::Merge.
 
Thanks - that does the trick. Though I am not familiar with hashes and ask for some further help to understand it.

I know - that eg. the key Assignment_Linux contains Values Linux_a and Linux_b.

How can I assess those values for further computing?
Thanks - Dietmar
---------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $conffile ="./config.cfg";
my %config;
my $config;
my $chapter;
sub get_config{
{
open(CONF,"<$conffile") or die "Cannot open $conffile\n";
while (<CONF>) {
next if /^\s*$/; # Blank line
next if /^\s*#/; # Comment
s/^\s*//; # Remove leading spaces
s/\s*$//; # Remove trailing spaces
if (m/^\[(\w+)]/) { # Find Chapter names
$chapter = $1;
} else {
push @{$config{$chapter}}, $_;
}
}
close (CONF);
}
}
&get_config();
my @KEY = keys %config;
print "@KEY\n";
-----------------------------------------------------
config.cfg

###Config File
[NodeGroupDefault]
1HOURHC
PRODUCTION
#
#
[CUSTOMER_GROUP]
customer_a=group_a
customer_b=group_b
customer_c=group_c
#
#
[Assignment_Linux]
Linux_a
Linux_b
#
[Assignment_Windows]
Windows_A
Windows_B
 
from example script and config aboth.
I added following to retrieve the keys of the hash

my @KEY = keys %config;
print "@KEY\n";

The return is:
#NodeGroupDefault CUSTOMER_GROUP Assignment_Windows Assignment_Linux

Then I try to read the value of eg "Assignment_Windows"

print "$config{'Assignment_Windows'}\n";

and the return is;
ARRAY(0x400977b0)

I'd expect this but instead
Windows_A Windows_B
to be printed.

Any idea.

 
I run in a regex problem

the regex from example above
if (m/^\[(\w+)]/) { # Find Chapter names

does not map lines with the - character in the name.
e.g.
[Chapter1]
is mapped but
[Chapter-2]
or
[Chapter-New-3]
is not mapped.
How doese the regex to be altered to map all lines
starting with a [
and ending with a ]

Valid Characters are A-Z a-z 0-9 - _
 
Code:
if (m/^\[([\w-]+)]/) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top