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
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..."
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();
> ...
> }
>
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_som
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
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
It looks like I missed the original message or something...but how about:
do_something(),do_something_else(),print"woohoo\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
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&&$
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 u
(Oops, got too happy with the ctrl+enter. Sorry Scott.)
> >
> >$a--&&do{do_something()};
> >
> >$a&&($a=0)||do{do_something()};
>
> These statements unconditionally alter $a (unless _I'm_ missing
> something). $a should only be set to 0/false iff $a is 1/true.
>
Well, if $a is false
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; } }
some
The more obscure looking the better...
$a&&$a--&&do_something();
Ah, but did you try running this? "$a--" will be false, meaning it will
short-circuit, and never get to do_something().
> -Original Message-
> From: disciple [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, May 22, 2002 04:35 PM
> To: [EMAIL PROTECTED]
> Subje
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();
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:
$a&&do_something();$a=0;
--Ala
mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, May 22, 2002 04:32 PM
> To: 'Fun With Perl ([EMAIL PROTECTED])'
> Subject: RE: shortest test for truth & false assignment
>
>
> Ah, but did you try running this? "$a--" will be false,
> meaning it
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&&--$a&&do_something();
--
Philippe BRUHAT - BooK
Fin
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
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.
H
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.
--
Move 1
pdcawley [11k]: This is, of course, where it all started to go wrong.
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
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
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 shoul
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 deme
I came up with another one. This also works for values of true other
than 1.
if ($a=~tr/.[^0]+/0/c) { do_something(); }
On Wed, 22 May 2002, Josh Goldberg wrote:
> how about
>
> do_something($a=0) if $a;
>
>
> On Wed, 22 May 2002, Scott Wiersdorf wrote:
>
> > My shortest try is this (10
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
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 ";
i
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 ?
>
>
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.
> > >
>
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. Thi
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
rsdorf [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 s
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
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
hi,
what about:
$a&&($a=&do_something&0)
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
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
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-
On Wed, May 22, 2002 at 03:58:05PM -0400, Patrick Gaskill wrote:
> > >$a&&($a=0)||do{do_something()};
> Anyway, the second bit of code there will short-circut after
> the first expression if $a is false, so everything after
> "$a&&" won't get executed, leaving $a alone.
But && has higher
38 matches
Mail list logo