the_slycer has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm sure this is an easy one, but I am getting confused.
I have some data like this:

bad stuff
more bad stuff
someother bad stuff

some good stuff
more good stuff

more good stuff

The question I have is, how to strip everything up to the first blank line, leaving me with the rest of it?

Replies are listed 'Best First'.
Re: strip until blank line
by quidity (Pilgrim) on Nov 23, 2000 at 00:04 UTC

    There is a very quick way to do this:

    { 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.
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 }
      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.

        Yah, it should. I think this behavior is documented in perlop.. look for the "range operator" (..) in a scalar context.
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
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
      Says lemming:
      > last if (/^+s$/);
      >
      > Whoops! Must learn to test.
      I think you still need to learn to test. That line matches the strings "s", "s\n", and nothing else.

      Maybe you wanted last if /^\s*$/. But I would have written last unless /\S/ instead.

      Using next here will start the next iteration of the while loop. You want to use last instead. Hmmm, which you've now done. This post looks silly now.

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.

      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.