數(shù)組類型并沒(méi)有明確定義為批處理腳本中的類型,但可以實(shí)現(xiàn)。 在批處理腳本中實(shí)現(xiàn)數(shù)組時(shí)需要注意以下幾點(diǎn)。
set
命令來(lái)定義。for
循環(huán)將需要遍歷數(shù)組的值。一個(gè)數(shù)組是通過(guò)使用下面的set
命令創(chuàng)建的。
set a[0]=1
其中0
是數(shù)組的索引,1
是分配給數(shù)組的第一個(gè)元素的值。
另一種實(shí)現(xiàn)數(shù)組的方法是定義一個(gè)值列表并遍歷值列表。 以下示例顯示了如何實(shí)現(xiàn)。
示例
@echo off
set list=1 2 3 4
(for %%a in (%list%) do (
echo %%a
))
以上命令產(chǎn)生以下輸出 -
1
2
3
4
可以使用下標(biāo)語(yǔ)法從數(shù)組中檢索值,并在數(shù)組的名稱后面立即傳遞要檢索的值的索引。
示例
@echo off
set a[0]=1
echo %a[0]%
在這個(gè)例子中,索引從0
開(kāi)始,第一個(gè)元素可以使用索引訪問(wèn)為0
,第二個(gè)元素可以使用索引訪問(wèn)為1
,依此類推。通過(guò)下面的例子來(lái)看看如何創(chuàng)建,初始化和訪問(wèn)數(shù)組 -
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
echo The first element of the array is %a[0]%
echo The second element of the array is %a[1]%
echo The third element of the array is %a[2]%
以上命令產(chǎn)生以下輸出 -
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3
要將一個(gè)元素添加到數(shù)組的末尾,可以使用set
元素以及數(shù)組元素的最后一個(gè)索引。
示例
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
Rem Adding an element at the end of an array
Set a[3]=4
echo The last element of the array is %a[3]%
以上命令產(chǎn)生以下輸出 -
The last element of the array is 4
可以通過(guò)在給定索引處指定新值來(lái)修改數(shù)組的現(xiàn)有元素,如以下示例所示 -
@echo off
set a[0]=1
set a[1]=2
set a[2]=3
Rem Setting the new value for the second element of the array
Set a[1]=5
echo The new value of the second element of the array is %a[1]%
以上命令產(chǎn)生以下輸出 -
The new value of the second element of the array is 5
遍歷數(shù)組是通過(guò)使用for
循環(huán)并遍歷數(shù)組的每個(gè)元素來(lái)實(shí)現(xiàn)的。以下示例顯示了一個(gè)可以實(shí)現(xiàn)數(shù)組的簡(jiǎn)單方法。
@echo off
setlocal enabledelayedexpansion
set topic[0]=comments
set topic[1]=variables
set topic[2]=Arrays
set topic[3]=Decision making
set topic[4]=Time and date
set topic[5]=Operators
for /l %%n in (0,1,5) do (
echo !topic[%%n]!
)
以下方面需要注意的事項(xiàng) -
set
命令專門定義。for
循環(huán)移動(dòng)范圍的/L
參數(shù)用于迭代數(shù)組。以上命令產(chǎn)生以下輸出 -
Comments
variables
Arrays
Decision making
Time and date
Operators
數(shù)組的長(zhǎng)度是通過(guò)遍歷數(shù)組中的值列表完成的,因?yàn)闆](méi)有直接的函數(shù)來(lái)確定數(shù)組中元素的數(shù)量。
@echo off
set Arr[0]=1
set Arr[1]=2
set Arr[2]=3
set Arr[3]=4
set "x=0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
)
echo "The length of the array is" %x%
以上命令產(chǎn)生以下輸出 -
The length of the array is 4
結(jié)構(gòu)也可以在批處理文件中使用一點(diǎn)額外的編碼來(lái)實(shí)現(xiàn)。 以下示例顯示了如何實(shí)現(xiàn)這一點(diǎn)。
示例
@echo off
set len=3
set obj[0].Name=Joe
set obj[0].ID=1
set obj[1].Name=Mark
set obj[1].ID=2
set obj[2].Name=Mohan
set obj[2].ID=3
set i=0
:loop
if %i% equ %len% goto :eof
set cur.Name=
set cur.ID=
for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do (
set cur.%%k=%%l
)
echo Name=%cur.Name%
echo Value=%cur.ID%
set /a i=%i%+1
goto loop
上面的代碼需要注意以下幾點(diǎn) -
set
命令定義的每個(gè)變量具有與數(shù)組的每個(gè)索引關(guān)聯(lián)的2
個(gè)值。i
設(shè)置為0
,以便可以遍歷結(jié)構(gòu)將數(shù)組的長(zhǎng)度為3
。i
的值是否等于len
的值,如果不是,則循環(huán)遍歷代碼。obj[%i%]
表示法訪問(wèn)結(jié)構(gòu)的每個(gè)元素。以上命令產(chǎn)生以下輸出 -
Name=Joe
Value=1
Name=Mark
Value=2
Name=Mohan
Value=3