Re: shortest test for truth false assignment

2002-05-28 Thread Ilmari Karonen
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

Re: shortest test for truth false assignment

2002-05-26 Thread Bart Lateur
On Wed, 22 May 2002 13:04:53 -0600, 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? For example, I have a global variable $a (given) that is somehow set to true. I want to test it for truth and

Re: shortest test for truth false assignment

2002-05-23 Thread Marcel Preda
hi, what about: $a($a=do_something0) PM http://marcell.hypermart.net/ Who cares what you think? - Original Message - From: Josh Goldberg [EMAIL PROTECTED] To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Thursday, May 23, 2002 7:44 AM Subject: RE: shortest test for truth false

Re: shortest test for truth false assignment

2002-05-22 Thread -Sx- IUDICIUM
I like - ($a =~ /$b/) ? do_something($a) : do_something($b); Unfortunately the shortest test may not be the right test. _Sx iudicium ferat: Perfer et obdura; dolor hic tibi proderit olim...

Re: shortest test for truth false assignment

2002-05-22 Thread Josh Goldberg
how about do_something($a=0) if $a; On Wed, 22 May 2002, Scott Wiersdorf wrote: My shortest try is this (10 characters w/o whitespace): if( $a%2 .. $a-- ) { do_something(); ... }

Re: shortest test for truth false assignment

2002-05-22 Thread Scott Wiersdorf
On Wed, May 22, 2002 at 01:04:53PM -0600, Scott Wiersdorf wrote: My shortest try is this (10 characters w/o whitespace): if( $a%2 .. $a-- ) { do_something(); ... } I put the ellipses in there after the do_something() because there's more to do after the

Re: shortest test for truth false assignment

2002-05-22 Thread Scott Wiersdorf
On Wed, May 22, 2002 at 01:04:53PM -0600, Scott Wiersdorf wrote: My shortest try is this (10 characters w/o whitespace): if( $a%2 .. $a-- ) { do_something(); ... } Ok, this is also 10 characters: if( $a=~s/.+// ) { ... } and has the (strange) benefit of only

RE: shortest test for truth false assignment

2002-05-22 Thread Patrick Gaskill
What about... $a--do{do_something()}; If $a isn't 1, you could do this, too: $a($a=0)||do{do_something()}; Right? I'm surprised noone's mentioned it, unless I'm missing something... Patrick

Re: shortest test for truth false assignment

2002-05-22 Thread Samy Kamkar
It looks like I missed the original message or something...but how about: do_something(),do_something_else(),printwoohoo\n,etc,if$a%2..$a--; It really depends on what you're doing, so this may not always work depending on how it's all structured -Samy Scott Wiersdorf wrote: On Wed, May 22,

Re: shortest test for truth false assignment

2002-05-22 Thread Adam Rice
Quoting Scott Wiersdorf ([EMAIL PROTECTED]): My shortest try is this (10 characters w/o whitespace): if( $a%2 .. $a-- ) { do_something(); ... } I'm a little confused, but I'm assuming you're allowed to stipulate what the true value is. In which case if ($a$a--) {

Re: shortest test for truth false assignment

2002-05-22 Thread Scott Wiersdorf
On Wed, May 22, 2002 at 03:36:06PM -0400, Patrick Gaskill wrote: What about... $a--do{do_something()}; If $a isn't 1, you could do this, too: $a($a=0)||do{do_something()}; Right? I'm surprised noone's mentioned it, unless I'm missing something... These statements

Re: shortest test for truth false assignment

2002-05-22 Thread Adam Rice
Quoting Scott Wiersdorf ([EMAIL PROTECTED]): if( $a%2 .. $a-- ) { do_something(); ... } I think my favourite is if (Vi$a) { do_something(); } Adam PS. Of course, you have to do something like sub Vi { if ($_[0]) { $_[0]=0; return 1; } else { return 0; } }

RE: shortest test for truth false assignment

2002-05-22 Thread disciple
heh or better still $a$a--do_something; :) -Original Message- From: disciple [mailto:[EMAIL PROTECTED]] Sent: 22 May 2002 21:35 To: [EMAIL PROTECTED] Subject: RE: shortest test for truth false assignment The more obscure looking the better... $a$a--do_something();

RE: shortest test for truth false assignment

2002-05-22 Thread Ala Qumsieh
Scott writes: 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? Hmmm .. so, after the test, no matter what the inital value of your variable is, it will be false. So: $ado_something();$a=0; --Ala

RE: shortest test for truth false assignment

2002-05-22 Thread Philippe 'BooK' Bruhat
On Wed, 22 May 2002, Patrick Gaskill wrote: Ah, but did you try running this? $a-- will be false, meaning it will short-circuit, and never get to do_something(). I think you are confusing $a$a--do_something(); with $a--$ado_something(); -- Philippe BRUHAT - BooK Financez le

Re: shortest test for truth false assignment

2002-05-22 Thread Simon Cozens
Scott Wiersdorf: if( $a%2 .. $a-- ) { I don't understand this. You see, if $a-- leaves $a as false, then: if ($a--) is the shortest way of solving the problem. And if not, then your code above won't work: % perl -le '$a=2;if( $a%2 .. $a-- ) {print Hi}; print $a' 2 (2 is, you will

Re: shortest test for truth false assignment

2002-05-22 Thread Ronald J Kimball
On Wed, May 22, 2002 at 10:45:39PM +0100, Simon Cozens wrote: Scott Wiersdorf: if( $a%2 .. $a-- ) { I don't understand this. You see, if $a-- leaves $a as false, then: if ($a--) is the shortest way of solving the problem. But that will change $a to true if it was false. Here's

Re: shortest test for truth false assignment

2002-05-22 Thread Ronald J Kimball
On Wed, May 22, 2002 at 10:55:52PM +0100, Simon Cozens wrote: Ronald J Kimball: if( $a%2 .. $a-- ) { Here's what I understand the goal to be: If $a is true, set $a to false and execute some code But the code above doesn't do that for all true values. Sorry, I left out a

RE: shortest test for truth false assignment

2002-05-22 Thread Alistair . McGlinchy
Scot et al, Just a point if clarification. The code we're supposed to golf must be funcationally equivalent to: if( $a ) { $a = 0; do_something(); } ## and now $a is false Where were we told the do_something couldn't mess with $a? In which case Ala's solution

Re: shortest test for truth false assignment

2002-05-22 Thread Scott Wiersdorf
On Wed, May 22, 2002 at 11:11:13PM +0100, [EMAIL PROTECTED] wrote: Scot. Is $a guaranteed to be untouched? Perhaps some context would help (and possibly present alternative solutions, though these have been really fun). Imagine something like this: ## $a is a flag that indicates we

Re: shortest test for truth false assignment

2002-05-22 Thread Yitzchak Scott-Thoennes
In article [EMAIL PROTECTED], Scott Wiersdorf [EMAIL PROTECTED] wrote: $a $a-- do { do_something(); ... } Two questions: a) can it be shortened to fewer characters? and b) is there another way to do it better in general? This is longer (not to mention demented) but

Re: shortest test for truth false assignment

2002-05-22 Thread Yanick
On Wed, May 22, 2002 at 10:06:39PM -0400, Josh Goldberg wrote: I came up with another one. This also works for values of true other than 1. if ($a=~tr/.[^0]+/0/c) { do_something(); } s/tr/s/, maybe ? (the transliterate operator doesn't use patterns, so the code

Re: shortest test for truth false assignment

2002-05-22 Thread Josh Goldberg
If tr doesn't use patterns then please help me understand how it works in my test cases? btw a straight swap to s/// fails for true == 1 but works for true == 'foo'. #!/usr/bin/perl for (1,'foo',0) { $a = $_; test(); test(); } sub test { print $a ; if

Re: shortest test for truth false assignment

2002-05-22 Thread Ronald J Kimball
On Wed, May 22, 2002 at 09:54:52PM -0400, Yanick wrote: On Wed, May 22, 2002 at 10:06:39PM -0400, Josh Goldberg wrote: I came up with another one. This also works for values of true other than 1. if ($a=~tr/.[^0]+/0/c) { do_something(); } s/tr/s/, maybe ? (the

Re: shortest test for truth false assignment

2002-05-22 Thread Josh Goldberg
ahhh, I get it now, thanks. On Wed, 22 May 2002, Ronald J Kimball wrote: On Wed, May 22, 2002 at 09:54:52PM -0400, Yanick wrote: On Wed, May 22, 2002 at 10:06:39PM -0400, Josh Goldberg wrote: I came up with another one. This also works for values of true other than 1. if

Re: shortest test for truth false assignment

2002-05-22 Thread Josh Goldberg
so then if($a=~tr/0/0/c) {do_something();} should work for any value of true, right? On Wed, 22 May 2002, Ronald J Kimball wrote: On Wed, May 22, 2002 at 09:54:52PM -0400, Yanick wrote: On Wed, May 22, 2002 at 10:06:39PM -0400, Josh Goldberg wrote: I came up with another one. This

Re: shortest test for truth false assignment

2002-05-22 Thread Yitzchak Scott-Thoennes
In article [EMAIL PROTECTED], Ronald J Kimball [EMAIL PROTECTED] wrote: if ($a=~tr/.[^0]+/0/c) { do_something(); } Actually, it translates every character that is *not* one of '.[^0]+' into '0'. Which means it does work, with slightly altered true/false semantics than are usual for perl.

RE: shortest test for truth false assignment

2002-05-22 Thread Tony . Young
Sorry, I just realised even that won't work as in modifies the state regardless. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Thursday, 23 May 2002 12:36 To: [EMAIL PROTECTED] Subject: RE: shortest test for truth false assignment Scott, I

RE: shortest test for truth false assignment

2002-05-22 Thread Josh Goldberg
if($a){do_something($a=0)} that's one stroke shorter On Thu, 23 May 2002 [EMAIL PROTECTED] wrote: I don't believe that you'll be able to shorten: if($a){$a=0;do_something()} any further unless you're able to use the flip-flop instead of $a: if($|--){do_something()} All suggestions so