> Schuyler's got it down to:
>
> perl -ne
'(/^\\s*[^#]|^\\s*#\\s*(include|define|(ifn?|un)def|else|elif|endif)/)&&prin
t'


I was trying to think about how to combine ifdef and undef. Unfortunately,
it really isn't possible. The above code won't match plain old "#if". Same
with his number 2.

As for an improvement on japhy's submission to handle "#  define FOO 1",
what about this 80 character submission:

perl -pe's/^\s*#\s*(?!\s|include|define|ifn?(def)?|else|elif|undef|endif).*/
/m' your_file

Japhy's original submission used /s, but I think he meant /m ...

Also note that all of the given submissions fail on:
'#defined this variable here for efficiency reasons'. Do they need to use
\b?

perl -pe
's/^\s*#\s*(?!\s|include|define|ifn?(def)?|else|elif|undef|endif)\b.*//m'
your_file

If this slightly-wronged-ness is okay, how far can we take it? :) Or is
correctness paramount? For example, this 79-er incorrectly matches '#un',
and '#undefine':

perl -pe's/^\s*#\s*(?!\s|include|(un|ifn?)(def)?(ine)?|else|elif|endif).*//m
'

You have one aspect of your problem which might confuse perl-golfers tho.
Can you use modules in your real-work task, even though they are disallowed
by the rules of perlgolf?

Given Strip.pm:
$cond =
qr/^\s*#\s*(?!\s|include|define|ifn?(def)?|else|elif|undef|endif).*/;

Then you can do it in much shorter:
perl -pMStrip -e's/$cond//s'

Of course, there may be reasons where you can't use files, in which case
nevermind. Or, you could lobby for this wonderfully named, designed,
documented, and test-including Strip.pm to be added to the core. :)

Mike Lambert

Reply via email to