Conditional expressions in a variable?

May 20, 2008
11,400
99
Syracuse, NY, USA
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:\>
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
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.
 
May 20, 2008
11,400
99
Syracuse, NY, USA
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).
 
Similar threads
Thread starter Title Forum Replies Date
D Conditional expressions Support 11
vefatica Documentation Help: links to "Conditional expressions" don't work Support 2
vefatica The conditional expressions "1" and "0"? Support 25
D conditional expression: force numeric Support 6
Joe Caverly Conditional Expression Test of @SFN Support 4
dcantor New conditional test in TCC 12.10 Support 2
Jesse Heines Using Regular Expressions with the REN commanc Support 8
F WAD SWITCH statement with regexes or wildcards as CASE expressions Support 9
mikea How to? Regular expressions in TCC Support 6
vefatica FFIND and multi-line regular expressions Support 4
vefatica Regular expressions? Support 12
T FOR command and regular expressions Support 19
Steve Pitts How to? FFIND, regular expressions and double quotes Support 4
vefatica Regular expressions in FOR's set? Support 10
vefatica DIR and regular expressions Support 0
T DEL disregards switch when using regular expressions Support 2
C searching history and regular expressions Support 5
D Parsing an array variable to an array does not work Support 5
sully9 How to? Unexpected %DATE% CMD variable format (vs DATE /T format) in CMDebug Support 1
P Inserting string into a variable. Support 3
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
R Difference between TCC and CMD: variable names with hyphens Support 4
Joe Caverly Get a variable type Support 2
M How to set Path variable for non-executable file extensions in 4DOS Support 8
rps For loop internal %_for_files variable? Support 2
A How to safely print variable's value without quotes? Support 9
vefatica Array name in a variable? Support 3
nikbackm TCC 20 variable expansion bug Support 6
X How to set %_INIREAD result to variable Support 5
Charles Dye _USBS variable Support 2
BeechComer Debugger: how to set %1 variable? Support 3
Mordachai Variable followed by function constantly failing... Support 5
T Variable indirection for arrays Support 1
Mike Hall Unexpected "Variable loop" in IF condition Support 10
vefatica If there's no PATH variable ... Support 5
C Maximum length for a variable Support 8
D WAD Internal variable _cpu is undefined Support 2
fpefpe How to? Global variable set /v Support 4
I Change value of a variable while debugging Support 3
I "IF" command in a variable does not expand redirection variable name Support 0
J Variable setting regression in tcc17 ? Support 5
Jay Sage WAD Variable _LOGFILE Not Working Support 2
D Fixed Change in behavior of variable expansion between 16 and 17 Support 3
rps Fixed Using last argument variable ! Support 5
Jay Sage Brackets Around Variable Names Support 11
D Internal variable _tctabs within TCExit Support 9
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
J Set environment variable from stdout of command Support 3
nickles Passing a variable to a command w/o adding it to the local environment Support 7

Similar threads