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 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





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 do something:

snip
$a = 1;

...

if( $a ) {
   $a = 0;
   do_something();
}

Shortest? Huh... I don't care, personally. But I sometimes do this,
which has not too much redundancy (it only mentions $a once, for
example):

$a = do { do_something(); 0 };

$a will become 0 afterwards, the do BLOCK is only executed if $a was
true. That was what you asked for, isn't it?

-- 
Bart.



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 assignment


 
 if($a){do_something($a=0)}
 that's one stroke shorter
 









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 do_something. It really is a block of statements
(though I don't care how they're executed, as long as it's done in
order). The ideas posted so far are excellent, but don't take the
blockish nature of this structure into account (of course, tmtowtdi).

Scott
-- 
Scott Wiersdorf
[EMAIL PROTECTED]



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 letting the empty string be
false (which is ok).

Scott
-- 
Scott Wiersdorf
[EMAIL PROTECTED]



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, 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 do_something. It really is a block of statements
 (though I don't care how they're executed, as long as it's done in
 order). The ideas posted so far are excellent, but don't take the
 blockish nature of this structure into account (of course, tmtowtdi).
 
 Scott
 


-- 
Samy Kamkar -- cp5 -- [EMAIL PROTECTED]
LucidX.com / LA.pm.org / code.LucidX.com




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--) {
   do_something();
 }
 
works, provided the true value is 1.

Adam

-- 
Adam Rice -- [EMAIL PROTECTED] -- Blackburn, Lancashire, England



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 unconditionally alter $a (unless _I'm_ missing
something). $a should only be set to 0/false iff $a is 1/true.

Scott
-- 
Scott Wiersdorf
[EMAIL PROTECTED]



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; } }
somewhere else in your program, but that's a small price to pay for a
splendid obfuscation.

-- 
Adam Rice -- [EMAIL PROTECTED] -- Blackburn, Lancashire, England



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 développement de Perl, avec YAS et les Mongueurs de Perl !
 http://www.mongueurs.net/association/actions/pdg2002.html




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 note, a true value)

But sticking to the spirit of the game:

 % perl -le '$a=2; if ($a..($a=0)) { print Hi }; print $a'
Hi
0

-- 
About the use of language: it is impossible to sharpen a pencil with a blunt
ax.  It is equally vain to try to do it with ten blunt axes instead.
-- Edsger Dijkstra



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 what I understand the goal to be:

If $a is true, set $a to false and execute some code
If $a is false, do nothing.


Ronald



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 condition.  You can choose what values to use for true
and false.  (As implied in the original message, It doesn't have to be
numeric (i.e., bit strings or any true/false values will work).)

Ronald



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 

$ado_something();$a=0

is the only one so far that works for all do_something()'s

Scot. Is $a guaranteed to be untouched?


Alistair
 --
 Alistair McGlinchy,   [EMAIL PROTECTED]
 Sizing and Performance, Central IT,   ext. 5012,   ph +44 20 7268-5012
 Marks and Spencer, 3 Longwalk Rd, Stockley Park, Uxbridge UB11 1AW, UK 
 


---


Registered Office:
Marks  Spencer p.l.c
Michael House, Baker Street,
London, W1U 8EP
Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422 
Facsimile (020) 7487 2670

www.marksandspencer.com

Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and 
then delete it from your system; you should not copy, disclose, or distribute its 
contents to anyone nor act in reliance on this e-mail, as this is prohibited and may 
be unlawful.

The registered office of Marks and Spencer Financial Services Limited, Marks and 
Spencer Unit Trust Management Limited, Marks and Spencer Life Assurance Limited and 
Marks and Spencer Savings and Investments Limited is Kings Meadow, Chester, CH99 9FB.




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 should do something
$SIG{HUP} = sub { $a++ };

while( 1 ) {
foo();
...
bar();
...

## check to see if we need to reread config file
if( data_is_stale() || $a ) {
$a = 0;
reread_config_file();
do_something_else();
do_yet_something_else();
}

## now $a is 0 and the above code won't get invoked again until
## someone sends us a HUP
}

For simplicity's sake, I reduced the problem to what Alistair quoted
above:

if( $a ) {
$a = 0;
do_something();
do_something_else();
...
}

Hence my original question: can the 'if( $a ) { $a = 0; ... }'
construct be done in fewer characters than my best try:

if( $a%2 .. $a-- ) {
do_something();
...
}

My example assumes the following constraints: $a is zero (or any even
number) to begin with. This next example doesn't assume digits, but it
does require the starting value of $a to be the empty string:

$SIG{HUP} = sub { $a='foo' };
$a = '';

while( 1 ) {
yada_yada();

if( $a=~s/.*// ) {
do_something();
...
}
}

I don't care what constraints you impose, as long as the block gets
executed exactly once and only when $a is true (for whatever you want
to define as true).

Shortest (looking) answer yet (which several people posted) is:

$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 has been a fun thread, btw (for me, anyway). Thanks.

Scott
-- 
Scott Wiersdorf
[EMAIL PROTECTED]



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 should work with any true value:

(($a)=$a?():$a)|| do { ... }



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 above change every instances of '.' by a 0,
of '[' by a '[' and so on and so forth...)


Joy,
`/anick

-- 
On the whole, human beings want to be good, 
but not too good, and not quite all the time.
 -- George Orwell



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 ($a=~tr/.[^0]+/0/cs) { do_something(); }
print $a\n;
}

sub do_something {
print  - ;
}

output:
1  - 0
0 0
foo  - 0
0 0

On Wed, 22 May 2002, 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 transliterate operator doesn't use patterns, so
   the code above change every instances of '.' by a 0,
   of '[' by a '[' and so on and so forth...)
 
 
 Joy,
 `/anick
 
 -- 
 On the whole, human beings want to be good, 
 but not too good, and not quite all the time.
  -- George Orwell
 




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 transliterate operator doesn't use patterns, so
   the code above change every instances of '.' by a 0,
   of '[' by a '[' and so on and so forth...)

Actually, it translates every character that is *not* one of '.[^0]+' into '0'.

/c complements the search list.

When the search list is longer than the replacement list, the last
character in the replacement class is repeated.

Ronald




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 ($a=~tr/.[^0]+/0/c) { do_something(); }
  
  s/tr/s/, maybe ?
  
  (the transliterate operator doesn't use patterns, so
  the code above change every instances of '.' by a 0,
  of '[' by a '[' and so on and so forth...)
 
 Actually, it translates every character that is *not* one of '.[^0]+' into '0'.
 
 /c complements the search list.
 
 When the search list is longer than the replacement list, the last
 character in the replacement class is repeated.
 
 Ronald
 
 




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 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 above change every instances of '.' by a 0,
  of '[' by a '[' and so on and so forth...)
 
 Actually, it translates every character that is *not* one of '.[^0]+' into '0'.
 
 /c complements the search list.
 
 When the search list is longer than the replacement list, the last
 character in the replacement class is repeated.
 
 Ronald
 
 




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.  (true means contains non-^,+,],[,.,0
character(s)).

Even the simplified:

if ($a=~y/0/0/c) { ... } 

has e.g. 00 as false when by usual perl semantics it is true.

You could just as well say:

if ($a=~y/x/x/c) { ... }

and have true mean contains non-x characters, false mean empty or only x's.



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 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 far have been longer.
 
 hth
 ty
 
  -Original Message-
  From: Scott Wiersdorf [mailto:[EMAIL PROTECTED]]
  Sent: Thursday, 23 May 2002 05:05
  To: [EMAIL PROTECTED]
  Subject: shortest test for truth  false assignment
  
  
  Hello fwp'ers,
  
  Here's a golfish problem that I thought some people might enjoy:
  
  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 do something:
  
  snip
  $a = 1;
  
  ...
  
  if( $a ) {
  $a = 0;
  do_something();
  }
  
  ## and now $a is false
  ...
  /snip
  
  You can't do this:
  
  if( $a = $a ? 0 : $a ) {
  do_something();
  ...
  }
  
  because the whole expression becomes false when $a is assigned 0.
  
  My shortest try is this (10 characters w/o whitespace):
  
  if( $a%2 .. $a-- ) {
  do_something();
  ...
  }
  
  Assuming warnings and stricture, can anyone find a shorter
  test/assignment? It doesn't have to be numeric (i.e., bit strings or
  any true/false values will work).
  
  Good luck!
  
  Scott
  -- 
  Scott Wiersdorf
  [EMAIL PROTECTED]
  
 



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 far have been longer.