in reply to chomp problem
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.
|
|---|