Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Conditional expressions in a variable?

May
13,215
180
I have a rather long conditional expression which must appear several times in a BTM file. My first, simple, attempt at putting a conditional expression in a variable failed (se below) is there any way to do it?

Code:
v:\> set condition=`%a == %b`
 
v:\> set condition
%a == %b
 
v:\> set a=1
 
v:\> set b=1
 
v:\> echo %condition
1 == 1
 
v:\> if %condition echo yes
 
v:\>
 
It's not possible with that syntax -- you're trying to nest IF expansion, which would break a few zillion batch files. I.e., what would happen with something like:

if "%condition"=="true" echo true

What you want it to do is to expand %condition, and then rewind the parser to the second word inside the first argument. The consequences everywhere else would be horrible.

Use %@IF -- that's what it was designed for.
 
It's not possible with that syntax -- you're trying to nest IF expansion, which would break a few zillion batch files. I.e., what would happen with something like:

if "%condition"=="true" echo true

What you want it to do is to expand %condition, and then rewind the parser to the second word inside the first argument. The consequences everywhere else would be horrible.

Use %@IF -- that's what it was designed for.
I wanted to test this (paraphrased, a win in tic-tac-toe, repreatedly)
Code:
(b0==b1 && b1==b2) || (b3==b4 && b4==b5) || (b6==b7 && b7==b8) || (b0==b3 && b3==b6) || (b1==b4 && b4==b7) || (b2==b5 && b5==b8) || (b0==b4 && b4==b8) || (b2==b4 && b4==b6)
In C, a macro works nicely and I was hoping to do something similar in a BTMfile. I wound up with a subroutine:
Code:
:winner
    if %b[0] == %b[1] .and. %b[1] == %b[2] return 1
    if %b[3] == %b[4] .and. %b[4] == %b[5] return 1
    if %b[6] == %b[7] .and. %b[7] == %b[8] return 1
    if %b[0] == %b[3] .and. %b[3] == %b[6] return 1
    if %b[1] == %b[4] .and. %b[4] == %b[7] return 1
    if %b[2] == %b[5] .and. %b[5] == %b[8] return 1
    if %b[0] == %b[4] .and. %b[4] == %b[8] return 1
    if %b[2] == %b[4] .and. %b[4] == %b[6] return 1
    return 0
and
Code:
 iff %@exec[gosub winner] EQ 1 then ...
This saves (some) unnecessary variable expansion as would occur with with the conditional expression all on one line (as it would be with %@IF or even my environment variable idea). Have you any ideas for making it faster?

History: Two complete idiots play tic-tac-toe, randomly choosing (from the empty squares) where to put their mark. What is the probability that such a game will result in a win? At first I MonteCarlo'd it ... in C, taking overnight, 10,000,000,000 games to get a solid 6-decimal place estimate. Then I simply played all 9! possible games and got the exact answer; that took a split second in C and about 4 hours with a batch file (just for kicks)). The answer, somewhat surprisingly, is exactly 55/63 (about .873).
 
Back
Top
[FOX] Ultimate Translator
Translate