On Wed, 22 May 2002, Scott Wiersdorf wrote:
>
> What is the shortest way to test a variable for truth and then set its
> value to false such that the truth test still succeeds?
>
> if( $a ) {
> $a = 0;
> do_something();
> }
I think you've got it. Since you specified the if-block structure as
fixed, and there has to be a reference to $a somewhere, there are only
five (5) non-whitespace strokes that could possibly be lost: $a=0;
If $a is mentioned twice, that leaves only three (3) strokes. That's
quite hard to beat. And I don't think there are any solutions short
enough that would only mention $a once, except for the clever Vi$a
trick. Unless $a is tied or something, which would be even further away
from the spirit of the challenge, I think.
Of course, there's this gem from the really-irresponsible-coding dept.:
*a = *|;
if ($a--) {
do_something();
}
Just make sure you don't select a different filehandle somewhere...
--
Ilmari Karonen - http://www.sci.fi/~iltzu/
"I admit, the original poster didn't say they wanted a solution that was
good. They just said they wanted it to be multi-threaded."
-- Logan Shaw in comp.lang.perl.misc