Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set multidimensional array batch

I am creating multidimensional arrays these way:

set Name[0]="name1"
set Id[0]=1234
set Action[0]=false

set Name[1]="name2"
set Id[1]=5678
set Action[1]=true

Then loop:

set "x=0"

:SymLoop
if defined Id[%x%] (
if defined Action[%x%] (
if defined Name[%x%] (

call set "name=%%Name[%x%]%%"
call set "id=%%Id[%x%]%%"
call set "action=%%Action[%x%]%%"

:: use name, id, action

)
set /a "x+=1"
GOTO :SymLoop
)
)
)

Is there some different approach to create multidimensional array without setting each key manually? When I define array and change order (key number), then I need to re-arange whole array to have right "key values".

Is there something like

set myArray ["name1", 1234, false]
set myArray ["name2", 5678, true]

and then iterate in loop and define - "name1" as "name" variable - 1234 set as "id" variable - "false" as "action" variable

in loop? Reducing use of "_x" variable definition

Note: I am novice at windows batch

like image 731
Ing. Michal Hudak Avatar asked Mar 12 '26 09:03

Ing. Michal Hudak


1 Answers

So simply add your array in a file:

Name1,1234,false
Name2,4567,true

then your batch:

for /f "tokens=1-3 delims=," %%i in (newfile.txt) do (
 echo name=%%i
 echo id=%%j
 echo action=%%k
)