Skipping blank lines while reading a flat file.
Perl 5.6 on linux Given this test file. --start- this is my file end-- I want to skip the blank lines and just print the lines with text, like this this is myfile This is my test case code. #!/usr/bin/perl -w use strict; my $opt_testfile=test-text.txt; open (TS, $opt_testfile) or die can't open file; while (TS) { chomp; next if ($_ =~ /^\s+/); print $_\n; } I'm not skipping the lines. My regex must be wrong. Can someone show me my error please? TIA DT -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Skipping blank lines while reading a flat file.
Dave, (this is one I know :-) ) I want to skip the blank lines and just print the lines with text, like this this is myfile This is my test case code. #!/usr/bin/perl -w use strict; my $opt_testfile=test-text.txt; open (TS, $opt_testfile) or die can't open file; while (TS) { chomp; next if ($_ =~ /^\s+/); next if ($_ =~ /^\s+/); You skip lines that BEGIN with a space. The REGEX you want is /^\s*$/ HTH Alois -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Skipping blank lines while reading a flat file.
Dave Thacker wrote: Perl 5.6 on linux Given this test file. --start- this is my file end-- I want to skip the blank lines and just print the lines with text, like this this is myfile This is my test case code. #!/usr/bin/perl -w use strict; my $opt_testfile=test-text.txt; open (TS, $opt_testfile) or die can't open file; while (TS) { chomp; next if ($_ =~ /^\s+/); print $_\n; } I'm not skipping the lines. My regex must be wrong. Can someone show me my error please? Either: next if /^\s*$/; Or: next unless /\S/; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Skipping blank lines while reading a flat file.
-Original Message- From: Dave Thacker [mailto:[EMAIL PROTECTED] Sent: Friday, October 07, 2005 07:05 To: beginners@perl.org Subject: Skipping blank lines while reading a flat file. Perl 5.6 on linux Given this test file. --start- this is my file end-- I want to skip the blank lines and just print the lines with text, like this this is myfile This is my test case code. #!/usr/bin/perl -w use strict; my $opt_testfile=test-text.txt; open (TS, $opt_testfile) or die can't open file; while (TS) { chomp; next if ($_ =~ /^\s+/); print $_\n; } I'm not skipping the lines. My regex must be wrong. Can someone show me my error please? TIA DT Try dropping the parens - next if $_ =~ /^\s+/; HTH - Ron -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Skipping blank lines while reading a flat file.
On Oct 7, Dave Thacker said: Given this test file. --start- this is my file end-- I want to skip the blank lines and just print the lines with text, like this When you say blank, do you mean lines with NO characters at all (other than the ending newline) or lines that ONLY contain whitespace? chomp; next if ($_ =~ /^\s+/); Ok, if the line is ONLY \n, then if you chomp() it, the line will be the empty string, . This means it WON'T be matched by /^\s+/. Your regex doesn't make any sense to me. It would skip lines that start with a space, like This one. And what's the use of looking for more than one leading space? I have a couple possible solutions for you. If you want to skip lines that consist of ABSOLUTELY NOTHING other than a newline, then you can do: while (FH) { chomp; next if $_ eq ; ... } If you want to skip lines that only have whitespace characters, then you can do: while (FH) { chomp; next unless /\S/; } -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response