以下是一个示例的BATCH脚本代码,用于交换数组的两个下标值:
@echo off
setlocal enabledelayedexpansion
REM 定义一个数组
set "array[0]=apple"
set "array[1]=banana"
set "array[2]=cherry"
REM 定义要交换的下标
set "index1=0"
set "index2=2"
REM 输出交换前数组的值
echo 交换前的数组:
for /l %%i in (0,1,2) do (
echo array[%%i]=!array[%%i]!
)
REM 交换数组下标值
set "temp=!array[%index1%]!"
set "array[%index1%]=!array[%index2%]!"
set "array[%index2%]=!temp!"
REM 输出交换后数组的值
echo 交换后的数组:
for /l %%i in (0,1,2) do (
echo array[%%i]=!array[%%i]!
)
该示例中,首先定义了一个名为array
的数组,然后定义要交换的两个下标index1
和index2
。接下来,使用set
命令将要交换的两个下标对应的值进行交换操作。最后,使用循环输出交换后数组的值。
运行该脚本,输出结果如下:
交换前的数组:
array[0]=apple
array[1]=banana
array[2]=cherry
交换后的数组:
array[0]=cherry
array[1]=banana
array[2]=apple
通过该示例,可以实现BATCH脚本中交换数组下标值的功能。