Can you use arrays in the simple windows batch programming? The answer is yes, but we have to use some tricks to emulate the arrays.
@echo off :: helloacm.com batch programming tutorials :: create arrays in batch set len=3 set obj[0]=A set obj[1]=B set obj[2]=C set i=0 :loop if %i% equ %len% goto :eof for /f "usebackq delims== tokens=2" %%j in (`set obj[%i%]`) do ( echo %%j ) set /a i=%i%+1 goto loop
We have to define variables using set and the length has to defined first. The above script will print A, B, C in three lines and we treat obj[0], obj[1] and obj[2] as three individual variables (their memory address locations are not continuous).
We can use set obj[%i%] to get strings for obj[0], obj[1], and obj[2] and we use the for to further evaluate the value. Base on the same principle, we can also create arrays of struct (records) like this:
@echo off :: helloacm.com batch programming tutorials :: create arrays of structures in batch set len=3 set obj[0].Name=Name1 set obj[0].Value=Value1 set obj[1].Name=Name2 set obj[1].Value=Value2 set obj[2].Name=Name3 set obj[2].Value=Value3 set i=0 :loop if %i% equ %len% goto :eof set cur.Name= set cur.Value= for /f "usebackq delims==. tokens=1-3" %%j in (`set obj[%i%]`) do ( set cur.%%k=%%l ) echo Name=%cur.Name% echo Value=%cur.Value% set /a i=%i%+1 goto loop
It creates an array of 3 elements (record) and prints out:
Name=Name1 Value=Value1 Name=Name2 Value=Value2 Name=Name3 Value=Value3
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Excel Sheet Column Number and Title Conversion in C++
Next Post: How to Obtain the Second Distinct Highest Record using SQL?