Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To give some context to my question, here is a test program:

use strict; use warnings; # Given an ini file, return a string containing section contents. # (Note that ini file comment lines start with a ;) sub get_section { my $fcontents = shift; # in: ini file contents string my $section = shift; # in: section name to get # Note that the regex below will find multiple sections; # it's terminated by the start of a new section or end of file. my $s = join( "", $fcontents =~ /^[ \t]*\[$section\](.*?)(?:\Z|^[ \ +t]*\[)/msg ); $s =~ s/^[ \t]+//mg; # remove leading whitespace from each line $s =~ s/[ \t]+$//mg; # remove trailing whitespace from each line $s =~ s/^\s+//; # remove leading whitespace $s =~ s/\s+$//; # remove trailing whitespace # Remove up to three trailing comment lines $s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s; return $s; } my $inifile_contents = <<'BUK_LIKES_SUNDIALS'; [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 BUK_LIKES_SUNDIALS my $section1 = get_section( $inifile_contents, 'MySection' ); print "This is the contents of MySection -------\n$section1\n"; my $section2 = get_section( $inifile_contents, 'AnotherSection' ); print "This is the contents of AnotherSection -------\n$section2\n";

Running the test program above produces:

This is the contents of MySection ------- ; This is a comment line for MySection fld1 = 'value of field 1' fld2 = 42 This is the contents of AnotherSection ------- ; another comment asfld=69

I added the code to remove trailing comment lines because I found, in practice, that trailing comment lines in a section tended to be unrelated to that section, rather they were usually header comment lines for the following section.

Though general suggestions for code improvements are welcome, my specific question relates to this eyesore:

# Remove up to three trailing comment lines $s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s;
that I am currently using to remove trailing comment lines from a section. What's a better way to do it?


In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found