Reputation: 214
I am trying to do the configuration file using Config::Simple
The configuration file i.e. new1.conf
[Section1]
param1=value1
param2=value2
[Section2]
param1=value1
param2=value2
[Section3]
param1=value1
param2=value2
here is my code
use Config::Simple;
$cfg = new Config::Simple(syntax => 'ini');
#
# Get Section Names
#
$cfg = Config::Simple->import_from('new.conf', \%Config) or die Config::Simple->error();
my @arr = ( keys %Config );
@arr1 = grep s/\..*//, @arr;
my %uniq;
@uniq{@arr} = ();
@sections = keys %uniq;
foreach my $secname (sort @sections)
{
print "section : $secname\n";
foreach (sort keys %Config)
{
print "$_ : $Config{$_}\n";
}
}
For this i am getting the output like this
section : Section1
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2
section : Section2
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2
section : Section3
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2
I am trying to compare the section names with paramaters in that respective section.
for that i am trying to write this code
foreach my $secname (sort @sections)
{
print "section : $secname\n";
foreach (sort keys %Config)
{
$var = grep { !/\b[A-Za-z0-9.].*[.]/ } @arr;
if($secname == $var)
{
print "$secname\n";
}
else
{
print "false\n";
}
#compare 'secname' vs 'dialer onboard'.xxx
#print "$_ : $Config{$_}\n";
}
}
This is not working for me. I stuck up here only.For this i am getting output like this
section : Section1
false
false
false
false
false
false
section : Section2
false
false
false
false
false
false
section : Section3
false
false
false
false
false
false
I cant be able to compare and display the section name with the respective params and values.
And finally i want output like this below.
section : Section1
Section1.param1 : value1
Section1.param2 : value2
section : Section2
Section2.param1 : value1
Section2.param2 : value2
section : Section3
Section3.param1 : value1
Section3.param2 : value2
or at least i want to display the params. I think i am doing something wrong in comparing params and section. I am not able to identify that.
Please somebody suggest me where i am going wrong Thanks in advance.
Upvotes: 1
Views: 1221
Reputation: 69314
You seem to be making this far more complicated than it needs to be.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Config::Simple;
my $cfg = Config::Simple->import_from('new1.conf', \my %config)
or die Config::Simple->error();
my $curr_section = '';
foreach (sort keys %config) {
my ($section, $param) = split /\./;
if ($section ne $curr_section) {
say "section : $section";
$curr_section = $section;
}
say "$_: $config{$_}";
}
It's even easier if you use a module that is better suited to dealing with INI files.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Config::INI::Reader;
my $cfg = Config::INI::Reader->read_file('new1.conf');
foreach my $section (sort keys %$cfg) {
say "section: $section";
foreach my $param (sort keys %{$cfg->{$section}}) {
say "$section.$param : $cfg->{$section}{$param}"
}
}
Oh, and your problem was caused by using the wrong kind of comparison operator. You want $secname eq $var
, not $secname == $var
. Adding use strict
and use warnings
to your code is always a good idea.
Upvotes: 2