in reply to Removing multiple trailing comment lines from a string

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:

#!/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.