I looked again at this thread and at
kcott's line by line solution at
Re: Removing multiple trailing comment lines from a string
I would also be thinking of parsing line by line instead of as string.
I've shown
another method for that.
Some general comments re: .ini files:
- I find the comments meaningless unless I have to
modify and re-write the .ini file.
- These kind of files often are a mixture of program generated
and user modified contents.
The comments can be very helpful to the user
although they are meaningless to the program. I am unsure that modifiying
the user typed in comments in any way is a good idea? Mileage varies.
- Getting a definition of what the file looks like exactly is
in my opinion important.
- Some files have an important "root" un-named section at the
beginning with maybe version number and stuff like that as well as sometimes
parameters which over-ride variables with the sections. Whoa! If you are writing or can influence the spec, I would recommend not doing that.
- Some Perl modules like Config Tiny
do a good job at getting the basic parms into an HoH. Something this code does not handle (nor others)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %HoA;
while (my $line=<DATA>)
{
my $next_line = process_section ($1) if $line =~ /\s*\[\s*(\S+)\s*\
+]/;
if (defined $next_line) {$line = $next_line; redo}
}
print Dumper \%HoA;
sub process_section
{
my $section = shift;
my $line;
while (defined ($line=<DATA>) and $line !~ /\s*\[\s*(\S+)\s*\]/)
{
chomp $line;
next if $line =~ /^\s*$/; # skip blank lines
push @{$HoA{$section}}, $line;
}
# delete the "trailing comments" in this [section] heading
my $comment;
while ($comment = pop @{$HoA{$section}} and $comment =~ /^\s*;/ ){}
+;
push @{$HoA{$section}}, $comment;
return $line;
}
=prints
$VAR1 = {
'AnotherSection' => [
'; another comment',
'asfld=69'
],
'MySection' => [
'; This is a comment line for MySection',
'fld1 = \'value of field 1\' ',
'fld2 = 42'
],
'WhitespaceSection' => [
' ; Comment starting with a tab'
+,
' ; Comment starting with a tab
+ and a space',
' ; Comment starting with a space',
'; Comment ending with a tab ',
'; Comment ending with a tab and a
+space ',
'; Comment ending with a space ',
' ; tab+space+comment+space+tab
+ ',
' ; space+tab+comment+tab+space
+ ',
'qwe=rty',
'asd=fgh'
]
};
=cut
__DATA__
; this is root section
a = 2
; some comment in root
b = 3
; some trailing comment in root
[MySection]
; This is a comment line for MySection
fld1 = 'value of field 1'
fld2 = 42
; This is the heading for AnotherSection
[AnotherSection]
; another comment
asfld=69
; Heading for WhitespaceSection
[WhitespaceSection]
; Comment starting with a tab
; Comment starting with a tab and a space
; Comment starting with a space
; Comment ending with a tab
; Comment ending with a tab and a space
; Comment ending with a space
; tab+space+comment+space+tab
; space+tab+comment+tab+space
qwe=rty
asd=fgh
; trailing 1
; tab + trailing 2
; space + trailing 3
; trailing 4
PS: my previous code explicitly allowed more than 3 trailing comments because I thought that was a requirement and was one of the "problems". I am also not sure why some .ini file comments should be ignored and others not? That is a strange thing to me.