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

Arrays 在Windows批处理文件中,如何对数组中的第(n+;1)个元素进行回显或索引?,arrays,batch-file,indexing,Arrays,Batch File,Indexing,我有一个批处理脚本,它收集数据并将其插入一个名为output的数组中,该数组的大小cnt声明为set“output[!cnt!]=%%a” 我在(13!cnt!)DO echo中为/L%%n的循环提供了一个迭代器!输出[%%n] 这将与 第一、第四、第七、第十元素等等 如何修改函数以响应以下序列, 第一、二、四、五、七、八、十、十一等元素 我正在DO echo的行中寻找一些东西!输出[%%n]!输出[%%n+1]但是我没有正确的语法。 任何其他解决方案也可以 这就是我在C编程中应该如何做到的 f

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

我在(13!cnt!)DO echo中为/L%%n的循环
提供了一个迭代器!输出[%%n]
这将与

第一、第四、第七、第十元素等等

如何修改函数以响应以下序列,

第一、二、四、五、七、八、十、十一等元素

我正在
DO echo的行中寻找一些东西!输出[%%n]!输出[%%n+1]但是我没有正确的语法。
任何其他解决方案也可以

这就是我在C编程中应该如何做到的

for(n=1;I如何修改函数以便按以下顺序进行回音,
您要做的是打印不是3的倍数的每个元素。最简单的方法是使用

伪代码

if %%n modulo 3 >= 1 then print something.
@echo off
setlocal enabledelayedexpansion
for /l %%n in (1 1 20) do (
rem print values where cnt is not a multiple of 3
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo %%n
    )
  )
endlocal
F:\test>test
1
2
4
5
7
8
10
11
13
14
16
17
19
20
@echo off
setlocal enabledelayedexpansion
rem collect data and insert into array
rem ...
rem print values where cnt is not a multiple of 3
for /l %%n in (1 1 !cnt!) do (
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo !output[%%n]!
    )
  )
endlocal
使用
set/a
的批处理文件算法中的模运算符为
%%

批处理文件示例(test.cmd)

if %%n modulo 3 >= 1 then print something.
@echo off
setlocal enabledelayedexpansion
for /l %%n in (1 1 20) do (
rem print values where cnt is not a multiple of 3
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo %%n
    )
  )
endlocal
F:\test>test
1
2
4
5
7
8
10
11
13
14
16
17
19
20
@echo off
setlocal enabledelayedexpansion
rem collect data and insert into array
rem ...
rem print values where cnt is not a multiple of 3
for /l %%n in (1 1 !cnt!) do (
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo !output[%%n]!
    )
  )
endlocal
输出

if %%n modulo 3 >= 1 then print something.
@echo off
setlocal enabledelayedexpansion
for /l %%n in (1 1 20) do (
rem print values where cnt is not a multiple of 3
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo %%n
    )
  )
endlocal
F:\test>test
1
2
4
5
7
8
10
11
13
14
16
17
19
20
@echo off
setlocal enabledelayedexpansion
rem collect data and insert into array
rem ...
rem print values where cnt is not a multiple of 3
for /l %%n in (1 1 !cnt!) do (
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo !output[%%n]!
    )
  )
endlocal
您可以在批处理文件中使用相同的技术

片段

if %%n modulo 3 >= 1 then print something.
@echo off
setlocal enabledelayedexpansion
for /l %%n in (1 1 20) do (
rem print values where cnt is not a multiple of 3
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo %%n
    )
  )
endlocal
F:\test>test
1
2
4
5
7
8
10
11
13
14
16
17
19
20
@echo off
setlocal enabledelayedexpansion
rem collect data and insert into array
rem ...
rem print values where cnt is not a multiple of 3
for /l %%n in (1 1 !cnt!) do (
  set /a _mod=%%n %% 3
  if [!_mod!] GEQ [1] (
    echo !output[%%n]!
    )
  )
endlocal

更一般的解决方案 这就是我在C编程中应该如何做到的

注:

  • 将起始值作为参数传递给批处理文件
  • 根据需要修改以输出数组值
用法:

F:\test>test 1
1 2
4 5
7 8
10 11
13 14
16 17
19 20

F:\test>test 2
2 3
5 6
8 9
11 12
14 15
17 18
20 21
F:\test>test 2 4
2 3
6 7
10 11
14 15
18 19

更普遍的解决办法 但是我在评论中说我想输出
2,3,6,7,10,11,14,15,18…

请注意,您的
c
解决方案将不会处理这种情况,即使您更改循环起始值

对于
循环,
需要有两个变量参数,一个用于起始值,一个用于增量

批处理文件示例(test.cmd):

用法:

F:\test>test 1
1 2
4 5
7 8
10 11
13 14
16 17
19 20

F:\test>test 2
2 3
5 6
8 9
11 12
14 15
17 18
20 21
F:\test>test 2 4
2 3
6 7
10 11
14 15
18 19
进一步阅读
  • -对于所有与Windows命令行相关的内容都是一个很好的参考
  • -有条件地对一系列数字执行命令
  • -有条件地执行命令
  • -显示、设置或删除CMD环境变量。使用set所做的更改将仅在当前CMD会话期间保留

    • 我将使用增量为3的循环

      @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 echo !arr[%%n]! %%arr[!idxAddOne!]%%
      

      将此循环中的步骤4更改为1
      for循环for/L%%n in(14!cnt!)执行echo!输出[%%n]!
      ==>
      for循环for/L%%n in(11!cnt!)执行echo!输出[%%n]!
      @Hackoo如果不起作用,它将回显每个值。您可以阅读:"若要在FOR/IF内的索引发生更改时获取元素的值,请将元素用双百分比符号括起来,并在命令前面加上
      call
      。实现上一过程的另一种方法是使用额外的FOR命令通过等效的可替换参数更改索引的延迟扩展,然后使用dela数组元素的扩展".这就是诀窍,但在本例中,我正在寻找一个更一般化的解决方案。假设循环必须从第二个元素开始,那么所需的序列将是
      2,3,6,7,10,11,14,15,18等等……
      让我把问题说得更清楚。@DanielFurtado你不能用那种方式概括。现在你正在打印2,跳过2,等等……这是另一个问题。@DanielFurtado另外,当你有答案时,请不要改变你的具体要求。这会使我的答案无效,当我用我的(空闲)时间解决你原来的问题时,这不是一件好事。如果你有新的要求,问一个新问题(并接受这个答案,因为它解决了你的问题)。这就是确切的原因,我没有在标题中提到我的原始问题中的顺序。我已经离开了(n+1)th元素毫无疑问,我非常感谢您的努力,它并不能完全满足我的要求。@DavidPostill您的广义解决方案可以很好地计算索引,但我认为OP的问题是从这样的索引中获取数组内容,因为这在batch@DanielFurtado你在对我的a的评论中说nswer“假设循环必须从第二个元素开始,然后期望的序列将是2,3,6,7,10,11,14,15,18等等。”这个答案没有解决这个问题,我的答案是…@DanielFurtado我真的不明白你从何而来。你打消了我的答案,因为“不过我正在寻找一个更广义的解决方案。”然后你接受了一个不是泛化的答案:/@DavidPostill…在这一点上,你的答案的“更泛化”部分也有我想要的。如果我可以,我肯定也会接受你的解决方案,但这个选项不存在。