gpt4 book ai didi

arrays - 在 Windows 批处理文件中,如何回显或索引数组中的第 (n+1) 个元素?

转载 作者:行者123 更新时间:2023-12-02 03:03:02 26 4
gpt4 key购买 nike

我有一个批处理脚本,它收集数据并将其插入到一个名为 output 的数组中,该数组的大小为 cnt 声明为 set "output[!cnt!]=%%a"

我有一个循环迭代器 for /L %%n in (1 3 !cnt!) DO echo !output[%%n]!这将回显

1st, 4th , 7th , 10th element and so on...

如何修改函数以回显以下序列,

1st,2nd,4th,5th,7th,8th,10th,11th element and so on....

我正在寻找类似 DO echo !output[%%n]! !output[%%n+1]! 的内容但我的语法不正确。任何其他解决方案也可以。

这就是我在 C 编程中的做法

for(n=1;i<cnt;n+=3){printf("%d %d",output[n],output[n+1])}

逐步如下:

Initially n=1 display output[1] output[2]

After one iteration n=4 display output[4] output[5]

After one more iteration n=7 display output[7] output[8]

我的别有用心是获得一个通用的语法,我们可以在其中显示第(n+m)元素 for(n=1;n<cnt;n+=3){printf("%d %d",output[n],output[n+m])}

该解决方案不需要包含 for 循环,do while 也可以。

最佳答案

我将使用增量为三的循环。

@echo off
setlocal EnableDelayedExpansion

REM Building a test array
for /L %%n in (1 1 12) do set "arr[%%n]=_%%n_"

for /L %%n in (1 3 12) do (
set /a idxAddOne=%%n+1
for %%m in (!idxAddOne!) do echo !arr[%%n]! !arr[%%m]!
)

主要问题是代码块内部的扩展。
可以使用 set/a idxAddOne=%%n+1 计算 idx+1,但使用新索引有点棘手,因为您需要数组的内容。
我在这里使用额外的 for 循环将索引复制到新的参数变量 %%m 中。
也可以通过CALL双重展开来解决。

call echo !arr[%%n]! %%arr[!idxAddOne!]%%

关于arrays - 在 Windows 批处理文件中,如何回显或索引数组中的第 (n+1) 个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com