Batch - How to add several variables to one variable? -
i have problem want add content of 1 parameter another. explain you. of code:
set /a a=%a1%+%a2%+%a3%+%a4%+%a5%+%a6%+%a7%+%a8%+%a9% set /a b=%b1%+%b2%+%b3%+%b4%+%b5%+%b6%+%b7%+%b8%+%b9% set /a c=%c1%+%c2%+%c3%+%c4%+%c5%+%c6%+%c7%+%c8%+%c9% set /a d=%d1%+%d2%+%d3%+%d4%+%d5%+%d6%+%d7%+%d8%+%d9% set testtheanswer= following lines wrong: if %a% neq 45 (then should add "a, " %testtheanswer% , of course same other ones.) echo %testtheanswer%
and @ end should like: "the following lines wrong: a, b, d ,". have option how in mind complicated... can me this? :) regards
ok. there several points here; first 1 set /a
command may work the names of variables, variable expansions not needed. should work:
set /a a=a1+a2+a3+a4+a5+a6+a7+a8+a9 set /a b=b1+b2+b3+b4+b5+b6+b7+b8+b9 set /a c=c1+c2+c3+c4+c5+c6+c7+c8+c9 set /a d=d1+d2+d3+d4+d5+d6+d7+d8+d9 set "testtheanswer= following lines wrong:" if %a% neq 45 set "testtheanswer=%testtheanswer% a," if %b% neq 45 set "testtheanswer=%testtheanswer% b," if %c% neq 45 set "testtheanswer=%testtheanswer% c," if %d% neq 45 set "testtheanswer=%testtheanswer% d," echo %testtheanswer:~0,-1%
however, collection of variables same name , changing subscript called "array", , advantages of arrays don't need explicitly write each element of array, write one element , change subscript via for
command. also, may make use of fact set /a
command allows perform several operations separating each 1 comma:
setlocal enabledelayedexpansion /l %%i in (1,1,9) set /a a+=a%%i, b+=b%%i, c+=c%%i, d+=d%%i set "testtheanswer= following lines wrong:" %%v in (a b c d) if !%%v! neq 45 set "testtheanswer=!testtheanswer! %%v," echo %testtheanswer:~0,-1%
for further details on array management in batch files, see: arrays, linked lists , other data structures in cmd.exe (batch) script
ps - question "add several variables 1 variable"; parameter different thing. suggest change topic title.
Comments
Post a Comment