Page 1 of 1

Checking a condition from a list of variables?

Posted: 11 Nov 2011 12:18
by Cat
This is part of a code I'm writing.

Code: Select all

choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1=%V1%-1
if %errorlevel%==2 set /a V2=%V2%+1
if %errorlevel%==3 set /a V1=%V1%+1
if %errorlevel%==4 set /a V2=%V2%-1
set target=A%V2%%V1%
if %target%==%check% echo invalid&goto grid


I want the condition to activate with %check% being set to A12, A34, A21, A09, and A19.
Is there any way to do this without coding several lines?

Re: Checking a condition from a list of variables?

Posted: 11 Nov 2011 14:57
by dbenham
Try this

Code: Select all

setlocal enableDelayedExpansion
choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1-=1
if %errorlevel%==2 set /a V2+=1
if %errorlevel%==3 set /a V1+=1
if %errorlevel%==4 set /a V2-=1
set target=A%V2%%V1%
set test=,A12,A34,A21,A09,A19,
if !test:,%target%,=! neq !test! echo invalid&goto grid

The code searches test for target and replaces with nothing. If the result does not match test then target was in test.
I also simplified your V1 and V2 math

Dave Benham

Re: Checking a condition from a list of variables?

Posted: 11 Nov 2011 18:01
by Cat
I'm getting "A12 was not expected at this time" and the window closes. :S

Re: Checking a condition from a list of variables?

Posted: 11 Nov 2011 18:20
by dbenham
Oops - I forgot some quotes in the last line:

Code: Select all

setlocal enableDelayedExpansion
choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1-=1
if %errorlevel%==2 set /a V2+=1
if %errorlevel%==3 set /a V1+=1
if %errorlevel%==4 set /a V2-=1
set target=A%V2%%V1%
set test=,A12,A34,A21,A09,A19,
if "!test:,%target%,=!" neq "!test!" echo invalid&goto grid


Dave Benham

Re: Checking a condition from a list of variables?

Posted: 11 Nov 2011 19:04
by Cat
That works perfectly, thanks :)