G'day eyepopslikeamosquito,

I can see what you've done to run the tests; however, I don't know how that translates to your real-world code. The solution I've provided below is substantially different from your code. The main differences are:

Here's "pm_1178405_ini_file_clean.pl":

#!/usr/bin/env perl -l use strict; use warnings; get_clean_ini_data(); for (qw{MySection AnotherSection WhitespaceSection}) { print "Contents of '$_':\n", get_section($_); } { my %section_lines_for; sub get_clean_ini_data { my $current_section; while (<DATA>) { s/^\s*(.*?)\s*$/$1/; next unless length; if (/^\[([^]]+)/) { my $new_section = $1; strip_trailing_comments($current_section); $current_section = $new_section; } else { push @{$section_lines_for{$current_section}}, $_; } } strip_trailing_comments($current_section); } sub strip_trailing_comments { my $section = shift; return unless defined $section; for my $i (reverse 0 .. $#{$section_lines_for{$section}}) { if (0 == index $section_lines_for{$section}[$i], ';') { pop @{$section_lines_for{$section}}; } else { last; } } } sub get_section { join "\n", @{$section_lines_for{$_[0]}} } } __DATA__ [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

Output:

$ pm_1178405_ini_file_clean.pl Contents of 'MySection': ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 Contents of 'AnotherSection': ; another comment asfld=69 Contents of '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

Because whitespace is difficult to see (especially differentiating spaces from tabs), I passed the script and output through `cat -vet`. I used this for my own testing; you might also find it useful. The relevant parts are in the spoiler.

— Ken


In reply to Re: Removing multiple trailing comment lines from a string by kcott
in thread Removing multiple trailing comment lines from a string by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.