w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
how to initialize array using for loop in batch file
@echo off
set /p process= How many process: 
:BT

echo BT

FOR /L %%A IN (1,1,%process%) DO (
 set /p bt[%%A]=
)

:AT

echo AT

FOR /L %%A IN (1,1,%process%) DO (
 set /p at[%%A]=
)

:TABLE
echo                       TABLE

echo PROCESS      AT                    BT
FOR /L %%A IN (1,1,%process%) DO (
CALL echo  P%%A         %%at[%%A]%%   %%bt[%%A]%%
)

:AWT

pause

GOTO :EOF

Here's a fixed version.

Notes: count is not necessary. %%A contains the index 1..%process%.

spaces on each side of the = in a string-assignment (such as a set /p) are significant, so the space between the bt and [ is removed.

Batch simply charges on through the lines of the program until it encounters goto, call or exit. The tests fo end-of-loop are thus not required - once the for loop ends, batch simply passes onto the next instruction line.

at[count] for instance is a variable name, same as whatever. at[%count%] means at[1]..at[??] depending on the value in count- but only outside of a "block" (parenthesised series of lines). Within a block, at[%count%] means at[??] where ?? is the value of count at the time the for statement was encountered. Thie may even be nothingatall - an empty string (ie at[], which is a valid variablename.)

The call echo method uses a parsing trick to first substitute for %%A (the loop-control or metavariable) then echo %at[2]%. Some people don't like using this method - it's slower than the "as-designed" methods and under rare esoteric conditions may not yield the correct result. There are two "official" methods of accessing the run-time or current value of a variable within a loop - setlocal and using a subroutine call - both of which are extensively documented on SO.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.