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


      

Reply via email to