Another one-liner?

Phil Carmody <[email protected]> Wed, 24 Jun 2009 13:02:33 +0000 (GMT)
Newsgroups gmane.comp.lang.perl.golf
Message-ID <[email protected]>
I needed to remove blank-line-separated chunks of code from a text file if those chunks contained any lines which were 'too long'. So in glorious hyper-verbose mode, I did the following:

#!/usr/bin/perl -wT

my $MAXLEN=65535;

my @chunk=();
my $maxlen=0;

while(<>)
{
    if(m/\S/)
    {
        if(length($_)>$maxlen) { $maxlen=length($_); }
        push(@chunk, $_) if($maxlen<$MAXLEN);
    }
    else
    {
        if($maxlen<$MAXLEN and @chunk)
        {
            print(@chunk, $_);
        }
        @chunk = ();
        $maxlen = 0;
    }
}

If you think how little it does, it's got to be one-linerable, no?

The 65535 is a bit arbitrary. Feel free to use anything between 1e5 and that.

Phil