Leona+ Dream for future

17Jun/091

Powerful Batch (4) Using Array


Batch commands doesn't support array, but it doesn't mean we can't simulate it.


Let's see the following code:

1
2
3
4
5
6
7
SET Obj_Length=2
 
SET Obj[0].Name=Test1
SET Obj[0].Value=Hello World
 
SET Obj[1].Name=Test2
SET Obj[1].Value=blahblah


We defined an array of a struct in the code above.


When accessing the value of an array element, we can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SET Obj_Index=0
 
:LoopStart
IF %Obj_Index% EQU %Obj_Length% GOTO :EOF
 
SET Obj_Current.Name=0
SET Obj_Current.Value=0
 
FOR /F "usebackq delims==. tokens=1-3" %%I IN (`SET Obj[%Obj_Index%]`) DO (
    SET Obj_Current.%%J=%%K
)
 
ECHO Name = %Obj_Current.Name%
ECHO Value = %Obj_Current.Value%
ECHO.
 
SET /A Obj_Index=%Obj_Index% + 1
 
GOTO LoopStart


Output:
Name = Test1
Value = Hello World

Name = Test2
Value = blahblah


Very cool, huh~~

Filed under: Language Leave a comment
Comments (1) Trackbacks (0)
  1. 顶你…..
    正需要…. 谢了哈


Leave a comment


No trackbacks yet.