參考一下,與我考慮的實(shí)現方法不一致。
http://www.cppblog.com/sleepwom/archive/2011/04/08/143671.html
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
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 /AObj_Index=%Obj_Index% + 1
GOTO LoopStart
--
輸出結果:
Name = Test1
Value = Hello World
Name = Test2
Value = blahblah
還有這個(gè),提供了數組的標準化過(guò)程。絕對是高手的作品。
http://blog.csdn.net/pengsixiang/article/details/5979900
Windows批處理 實(shí)現數組方法
Windows的批處理沒(méi)有提供數組這個(gè)數據結構,所以自己手動(dòng)編寫(xiě)了Bat相關(guān)的數組過(guò)程,方便在Bat中使用數組這個(gè)基本數據結構。表達式類(lèi)似C語(yǔ)言的數組。
使用數組,如下格式:
Set 變量名[下標]=值
下面是數組的相關(guān)函數
Array
初始化一個(gè)數組。
格式 Array 輸入_數組名字
FreeArray
釋放數組空間。
格式 FreeArray 輸入_數組名
CopyArray
復制一個(gè)數組
格式 CopyArray 輸入_源數組名 輸入_目的數組名 [元素個(gè)數]
InArray
判斷元素是否在數組中
格式 InArray 輸入_數組名 輸入_元素值
Libarray.cmd
set max=65535
set debug=
set debugerror=1
set deglev_1=error
set debugwarn=3
set deglev_3=warn
set debuginfo=7
set deglev_7=info
set debug_log=
set debug_log_dir=C:\
set debug_log_file=debug.log
set ver=1
:PrintError
if "%~1" == "" ( set out_text=Unkown Error ... ) else ( set out_text=%~1 )
echo %out_text%
goto :eof
:Debug
if defined debug (
if %~1 LEQ debug (
echo [ debug_deglev_%~1 ] %~2 %~3
if defined debug_log ( echo [ debug_deglev_%~1 ] %~2 %~3 >> %debug_log_dir%\%debug_log_file% )
)
)
goto :eof
:Array
rem Array <string array_name>
rem return -1 if not successful. return 0 if successful
set return=
if "%~1" == "" (
set return=-1
call :Debug "%debugerror%" "STD_Array" "array_name="%~1" "
goto :eof
)
if defined %~1_c (
call :FreeArray "%~1"
if not return == 0 (
call :Debug "%debugerror%" "STD_Array" "Fun_FreeArray_Return: %return%"
set return= -1
goto :eof
)
)
set %~1_c=0
set return=0
call :Debug "%debuginfo%" "STD_Array" "reutrn=%return%"
goto :eof
:FreeArray
rem FreeArray <string array_name>
rem return 0 if successful. return -1 array_name not vaild, return -2 array not vaild
set return=
if "%~1" == "" (
set return=-1
call :Debug "%debugerror%" "STD_FreeArray" "array_name="%~1" "
goto :eof
)
if not defined %~1_c (
set return=-2
call :Debug "%debugerror%" "STD_FreeArray" "not vaild array_count"
goto :eof
)
for /l %%a in ( 1, 1, %count% ) do (
set %~1_%%a=
)
set %~1_c=
set return=0
call :Debug "%debuginfo%" "STD_FreeArray" "reutrn=%return%"
goto :eof
:CopyArray
rem CopyArray <string from_array_name> <string to_array_name> [int count]
rem return 0 if successful, return -1 array_name not vaild, return -2 array not vaild
set return=
if "%~1" == "" (
set return=-1
call :Debug "%debugerror%" "STD_CopyArray" "from_array_name="%~1"
goto :eof
)
if not defined %~1_c (
set return=-2
call :Debug "%debugerror%" "STD_CopyArray" "not vaild array_count"
goto :eof
)
if "%~2" == "" (
set return=-1
call :Debug "%debugerror%" "STD_CopyArray" "to_array_name="%~2"
goto :eof
)
if "%~3" == "" ( set count=!%~1_c! ) else ( set count=%~3 )
if %count% GTR !%~1_c! (
set count=!%~1_c!
call :Debug "%debugwarn%" "STD_CopyArray" "count[ %~3 ] > from_array_count[ !%~1_c! ] set count = from_array_count"
)
for /l %%a in ( 1, 1, %count% ) do (
set %~2_%%a=!%~1_%%a!
)
set %~2_c=%count%
set return=0
call :Debug "%debuginfo%" "STD_CopyArray" "reutrn=%return%"
goto :eof
:InArray
rem InArray <string array_name> <string value>
rem return 0 if successful, return -1 array_name not vaild, return -2 value not vaild
set return=
if "%~1" == "" (
set return=-1
call :Debug "%debugerror%" "STD_InArray" "array_name="%~1""
goto :eof
)
if "%~2" == "" (
set return=-1
call :Debug "%debugerror%" "STD_InArray" "value="%~2""
goto :eof
)
if not defined %~1_c (
set return=-2
call :Debug "%debugerror%" "STD_InArray" "not vaild array_count"
goto :eof
)
for /L %%a in ( 1, 1, !%~1_c! ) do (
if "%~1_%%a" == "%~2" (
set return=%%a
goto :eof
)
)
set return=0
call :Debug "%debuginfo%" "STD_InArray" "reutrn=%return%"
goto :eof
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。