Front page | perl.golf |
Postings from June 2009
Another one-liner?
Thread Next
From:
Phil Carmody
Date:
June 24, 2009 06:02
Subject:
Another one-liner?
Message ID:
200184.17104.qm@web25406.mail.ukl.yahoo.com
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
Thread Next
-
Another one-liner?
by Phil Carmody