I have 36 different variables whose names have different lengths, some have numbers, others do not.
I need to see if any two have the same value.
Is there a way to do this without a giant if/else statement?
Thanks,
BatNoob
Variable names
Moderator: DosItHelp
Re: Variable names
There are multiple ways to do this; some are faster some are slower, but there are different limitations:
Could you describe the variable names and contents a little bit more detailed?
Or maybe this very slow algorithm may help you; although this is something similar to the "giant if/else statement" (sketched; untested):
penpen
Could you describe the variable names and contents a little bit more detailed?
Or maybe this very slow algorithm may help you; although this is something similar to the "giant if/else statement" (sketched; untested):
Code: Select all
@echo off
setlocal enableExtensions disableDelayedExpansion
set "VariableName1=A"
set "AnotherVariableVariableName2=B"
set "Name3=A"
:: ...
set ^"Collection="VariableName1" "AnotherVariableVariableName2" "Name3"^"
setlocal enableExtensions enableDelayedExpansion
for %%a in (%Collection%) do for %%b in (!Collection^:*"%%~a"^=!) do (
if "!%%~a!" == "!%%~b!" echo(Match found: "%%~a", "%%~b"
)
goto :eof
penpen
Re: Variable names
I assume you may name these 36 variables in any way you wish. If so, then the simplest/fastest way to solve this problem is creating an array, so the 36 variables could be processed in a loop:
If you can not name the 36 variables with similar names because they are not related, then the obvious question is: why you want to compare the values of unrelated variables?
Antonio
Code: Select all
setlocal EnableDelayedExpansion
rem The name of the 36 variables is v1, v2, ..., v36
set "match="
for /L %%i in (1,1,35) do if not defined match (
set /A j=i+1
for /L %%j in (!j!,1,36) do (
if "!v%%i!" == "!v%%j!" set "match=%%i and %%j"
)
)
if defined match echo Match found at variables %match%
If you can not name the 36 variables with similar names because they are not related, then the obvious question is: why you want to compare the values of unrelated variables?
Antonio
Re: Variable names
I am making a chess game. I realize that there is already one, but mine has color.
These are the variable names:
These are the variable names:
Code: Select all
BKS
WKS
BQS
WQS
BB1S
WB1S
BB2S
WB2S
BH1S
WH1S
BH2S
WH2S
BR1S
WR1S
BR2S
WR2S
B1S
B2S
B3S
B4S
B5S
B6S
B7S
B8S
W1S
W2S
W3S
W4S
W5S
W6S
W7S
W8S
Re: Variable names
batnoob wrote:but mine has color.
Er ... black and white? What other colors would be suitable?
Code: Select all
@echo off &setlocal
set "varnames=BKS WKS BQS WQS BB1S WB1S BB2S WB2S BH1S WH1S BH2S WH2S BR1S WR1S BR2S WR2S B1S B2S B3S B4S B5S B6S B7S B8S W1S W2S W3S W4S W5S W6S W7S W8S"
:: your code here ....
call :check_equal && (echo no equal values found) || (echo equal values found)
pause
exit /b
:check_equal
setlocal EnableDelayedExpansion
for %%i in (%varnames%) do for %%j in (%varnames%) do if "%%i" neq "%%j" if "!%%i!" equ "!%%j!" endlocal &exit /b 1
endlocal &exit /b 0
Question is why you want to compare every value instead of only the value of the variable that was changed?
Steffen
Re: Variable names
Depending on the variable content, you may reverse variable name and content, or use helping variables of that kind.
I assume the variable content of the variables in {"BKS", ..., "W8S"} is {"11", ..., "88"} (or {"A 1", ..., "H 8"}, or similar).
So you then need to do only one comparison to test, if two pieces end up on the same field (which i assume is what you are searching for).
This might help you:
penpen
I assume the variable content of the variables in {"BKS", ..., "W8S"} is {"11", ..., "88"} (or {"A 1", ..., "H 8"}, or similar).
So you then need to do only one comparison to test, if two pieces end up on the same field (which i assume is what you are searching for).
This might help you:
@echo off
setlocal enableExtensions enableDelayedExpansion
:: initial setup, using the following notation like yours (i'm unsure about the notation you are using):
:: K=king, Q=queen, R=rook, B=bishop, K=knight, P=pawn
:: [color][piece][index]
:: color in {B, W}, piece in {K, Q, R, B, K, P} count in [0..9]
:: numbers are coded field positions, so "12" is mapped to "A 2".
set /A "BR0=18, BK0=28, BB0=38, BQ0=48, BK0=58, BB1=68, BK1=78, BR1=88"
set /A "BP0=17, BP1=27, BP2=37, BP3=47, BP4=57, BP5=67, BP6=77, BP7=87"
set /A "WP0=12, WP1=22, WP2=32, WP3=42, WP4=52, WP5=62, WP6=72, WP7=82"
set /A "WR0=11, WK0=21, WB0=31, WQ0=41, WK0=51, WB1=61, WK1=71, WR1=81"
::use helper variables
:: note: Depending on your design it is possible to use these helper variables only and get rid of the above ones.
for %%a in (
BR0 BK0 BB0 BQ0 BK0 BB1 BK1 BR1
BP0 BP1 BP2 BP3 BP4 BP5 BP6 BP7
WP0 WP1 WP2 WP3 WP4 WP5 WP6 WP7
WR0 WK0 WW0 WQ0 WK0 WW1 WK1 WR1
) do set "F!%%~a!=%%~a"
:: change field (simulated gameplay without capturing any piece)
:: note you have to change the old helper variable (undefine it), the new helper variable, and the new variable content
:: 1: e4 d5
set "F52="
set "WP4=54"
set "F54=WP4"
set "F47="
set "BP3=45"
set "F45=BP3"
:: exemplary test if field is occupied by any piece:
if defined F45 echo field is occupied by !F45!.
goto :eof
penpen
Re: Variable names
@aGerman, in my chess game the board is the colors it is supposed to be, alternating black and white squares.
thank you for your help, although @penpen gave me a better answer
(Also, I didn't think of that until you said it. )
@penpen, thank you for your help.
thank you for your help, although @penpen gave me a better answer
(Also, I didn't think of that until you said it. )
@penpen, thank you for your help.