Re: strip until blank line
by quidity (Pilgrim) on Nov 23, 2000 at 00:04 UTC
|
{
local $/ = ""; # set to paragraph mode
$bad_stuff = <FILE>; # reads in until first blank line
}
# .. $/ is then returned to it's previous
# value, and you can carry on reading from
# <FILE> line by line.
| [reply] [d/l] |
Re: strip until blank line
by Fastolfe (Vicar) on Nov 23, 2000 at 00:22 UTC
|
A more idiomatic way of doing this:
while (<INPUT>) {
next if (1 .. /^$/); # skip header
# do something with body
}
| [reply] [d/l] |
|
|
Should this regex include whitespace?
while (<INPUT>) {
next if (1 .. /^\s*$/);
# yada yada
}
Also, your example reminds me of some kind of tutorial or
perldoc reference -- is that right? If so, could you
point me to it? This is a slick construction.
| [reply] [d/l] |
|
|
Yah, it should. I think this behavior is documented in perlop.. look for the "range operator" (..) in a scalar context.
| [reply] |
Re: strip until blank line
by steveAZ98 (Monk) on Nov 23, 2000 at 00:14 UTC
|
This will strip everything up to, but not including the first blank line. You can modify it to include the first blank if that is what you actually need.
HTH
#!/usr/bin/perl -w
my $x = 0;
while(<DATA>) {
$x = 1 if /^\s+$/;
next if $x != 1;
print;
}
__DATA__
bad stuff
more bad stuff
someother bad stuff
some good stuff
more good stuff
more good stuff
| [reply] [d/l] |
Re: strip until blank line
by lemming (Priest) on Nov 22, 2000 at 23:56 UTC
|
assuming FILE as your handle
while(<FILE>) {
last if (/^\s+$/);
}
# Begin processing
Whoops! First try would of just thrown away the first few lines. It works now thanks to quidity
Must learn to test.
And read the preview. Cut and paste got rid of the "\". Thanks dominus whose /\S/ solution would probably be quicker | [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
Re: strip until blank line
by kilinrax (Deacon) on Nov 23, 2000 at 00:04 UTC
|
This will strip all the lines preceeding the blank one:
my @data = <FILE>;
while( $data[0] =~ /\S/ ) {
shift @data;
}
print @data;
Update:
Just spotted quidity's post after fixing the error (d'oh), and the second point he raises is a good one; the following code would be better if you're reading from a file (the above code was designed to work whether you were working with data in a file or array, as you did not specify which you were dealing with):
while( $line = <FILE> ) {
last unless $line =~ /\S/;
}
which will leave you with FILE at the first line after the blank one. | [reply] [d/l] [select] |
|
|
This will not work, as you are not shifting from what you think. Shift defaults to @_ or @ARGV depending on your current scope. You want to use:
shift @data;
which you now have. You don't really though, as this solution (as a whole) requires you to read the entire file into memory, which could be a bad thing.
| [reply] [d/l] |