Typically when I am reading in data from a file where there is one "record" per line. I use the replacement constructs, =~ s/\A\s+//s; and =~ s/\s+\Z//s; this strips all white space characters off the beginning and ending of the given line and avoids pesky errors caused by buffering white space characters at the start or end of a line

Thus, in the case where I had the file handle FH open to an input file and I was populating the list @contents with one entry for each line in the file, I would have the following:

my @contents; my $line; while ($line = <FH>) { $line =~ s/\A\s+//s; $line =~ s/\s+\Z//s; push @contents, ( $line ); }

The above code snippet will populate @contents with one entry for each line from the file being read in via the file handle FH. Each line has the white space removed from the beginning of the line via execution of the replacement statements $line =~ s/\A\s+//s; and $line =~ s/\s+\Z//s;. \A at the start of a pattern matches the absolute begining of a string even if the string contains vertical space characters. \Z at the end of a pattern matches the absolute end of a string even if the string contains vertical space charactes. \s matches an occurence of any white space character: \f, \r, \n, space and tab. Use of the + after \s causes the pattern to match one or more white space characters. The s qualifier at the end of the replacement statement $line =~ s/\A\s+//s; , $line =~ s/\s+\Z//s; causes the string in $line to be treated as one line on which to perform pattern matching even if the string contains vertiacl space characters.

The approach I have suggested will work, although it is a little meticulous, and could be over kill for what you are trying to accomplish.


In reply to Re: chomp problem by jira0004
in thread chomp problem by piranhas

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.