批处理 - 数组

批处理 - 数组 首页 / 批处理入门教程 / 批处理 - 数组

数组在批处理脚本中没有明确定义为一种类型,但可以实现,在批处理脚本中实现数组时,需要注意以下事项。

  • 需要使用set命令定义数组的每个元素。
  • 需要" for"循环来遍历数组的值。

创建数组

使用以下set命令创建一个数组。

set a[0]=1

其中0是数组的索引,而1是分配给数组第一个元素的值。

实现数组的另一种方法是定义一个值列表并遍历值列表,以下示例显示了如何实现。

@echo off 
set list = 1 2 3 4 
(for %%a in (%list%) do ( 
   echo %%a 
))

上面的命令产生以下输出。

1
2
3
4

访问数组

您可以使用下标语法从数组中检索值,在数组名称后紧跟的方括号内传递要检索的值的索引。

@echo off 
set a[0]=1 
echo %a[0]%

在此示例中,索引从0开始,这意味着可以使用index为0来访问第一个元素,可以使用index为1来访问第二个元素,依此类推。让无涯教程检查以下示例以创建,初始化和访问数组-

链接:https://www.learnfk.comhttps://www.learnfk.com/batch-script/batch-script-arrays.html

来源:LearnFk无涯教程网

@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]%

上面的命令产生以下输出。

The first element of the array is 1 
The second element of the array is 2 
The third element of the array is 3

修改数组

要将元素添加到数组的末尾,可以将set元素与数组元素的最后一个索引一起使用。

@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]%

上面的命令产生以下输出。

The last element of the array is 4

您可以通过在给定索引处分配新值来修改Array的现有元素,如以下示例所示-

@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]%

上面的命令产生以下输出。

The new value of the second element of the array is 5

遍历数组

通过使用" for"循环并遍历数组的每个元素来实现数组的迭代。以下示例显示了可以实现数组的简单方法。

@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]! 
)

关于上述程序,需要注意以下几点:

  • 需要使用set命令专门定义数组的每个元素。

  • 带有/L参数的" for"循环用于遍历范围,用于遍历数组。

上面的命令产生以下输出。

Comments 
variables 
Arrays 
Decision making 
Time and date 
Operators

数组长度

数组的长度是通过迭代数组中的值列表来完成的,因为没有直接函数来确定数组中元素的数量。

@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%

输出 上面的命令产生以下输出。

The length of the array is 4

创建结构

也可以使用一些额外的实现代码在批处理文件中实现结构,以下示例显示了如何实现此目的。

@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

关于以上代码,需要注意以下关键事项。

  • 使用set命令定义的每个变量都有2个与数组的每个索引关联的值。

  • 变量 i 设置为0,以便可以遍历结构,将数组的长度设为3。

  • 始终检查i的值是否等于 len 的条件,否则,无涯教程将遍历代码。

  • 能够使用obj [%i%]表示法访问结构的每个元素。

上面的命令产生以下输出。

Name=Joe 
Value=1 
Name=Mark 
Value=2 
Name=Mohan 
Value=3

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

软件测试52讲 -〔茹炳晟〕

MySQL实战45讲 -〔林晓斌〕

软件工程之美 -〔宝玉〕

Redis源码剖析与实战 -〔蒋德钧〕

攻克视频技术 -〔李江〕

eBPF核心技术与实战 -〔倪朋飞〕

快手 · 移动端音视频开发实战 -〔展晓凯〕

现代C++20实战高手课 -〔卢誉声〕

好记忆不如烂笔头。留下您的足迹吧 :)