【问题标题】:Access Item from Array In Windows Batch File within For Loop在 For 循环中从 Windows 批处理文件中的数组访问项目
【发布时间】:2018-07-05 12:37:23
【问题描述】:

我试图迭代 Windows 批处理文件中的列表,在每次迭代中使用不同列表中的不同数字作为参数。

set input_image_list=(1 2 3 4 5 6 7 8 9 )

set output_image_list = (2 3 4 5 6 7 8 9 10)

set next_needed_image = (002 003 004 005 006 007 008 009 010)

FOR %%A IN (2 3 4 5 6 7 8 9 10)  DO python batchLayer4ThirdVideo.py --input_image !input_image_list[%%A]! --next_undreamed_image !next_needed_image[%%A]! --output_image %%A

但是当我将它们用作参数时,列表的索引不起作用,它只是将索引(例如input_image_list[2])作为我的参数,而它应该只是一个数字。

如何正确索引列表,以便将其中的每个数字作为参数传递?

【问题讨论】:

  • 您尚未创建任何数组,您已将名为 %input_image_list% 的变量设置为字符串值 (1 2 3 4 5 6 7 8 9 ),将名为 %output_image_list<space>% 的变量设置为字符串值 <space>(2 3 4 5 6 7 8 9 10) 和将名为 %next_needed_image<space>% 的变量转换为字符串值 <space>(002 003 004 005 006 007 008 009 010)
  • 谢谢,那么我该如何创建实际的数组呢?我上面使用的是大多数页面告诉我在批处理文件中创建数组
  • 从技术上讲,这将是题外话,因为它是对教程/参考信息的请求。此外,根据我现在提供的信息,您需要完整的 rewrite your question 和标题,并提供更新的代码,显示您对任务的尝试以及对尝试它们时发生的情况的解释。
  • 怎么跑题了?我问题的最后一行字面意思是“我如何正确索引列表,以便其中的每个数字都作为参数传递?”这正是我现在要问你的问题。
  • Windows 命令提示符或批处理脚本中没有数组!有伪数组,不过是普通的环境变量;所以set VAR=Valueset ARR[0]=Element 是技术上等价的变量,分别命名为VARARR[0]。所以写set "ARR=(0 1 2 3)" 不会创建一个可以像!ARR[0]!!ARR[%idx%]! 那样被索引的数组,它只创建一个名为ARR 并包含(0 1 2 3) 的变量。参考这篇很棒的帖子了解更多详情:Arrays, linked lists and other data structures in cmd.exe (batch) script

标签: python arrays windows batch-file indexing


【解决方案1】:

有一个(不知何故扭曲)technique 将字符串列表扩展为数组,方法是用计数和集合替换列表元素之间的空格(或任何其他字符)以形成数组。

从您的列表和 for 中,不清楚您是否需要基于 0 或 1 的索引。
(更简单的是for /l %%A in (2,1,10) Do ...

为了更好地理解,我使用了较短的变量名并将分割后的 python 命令回显到多行。

:: Q:\Test\2018\07\05\SO_51191502.cmd
@Echo off & SetLocal EnableDelayedExpansion

set i=0&Set "ImgIn= 1 2 3 4 5 6 7 8 9 10"
Set "ImgIn=%ImgIn: ="&Set /a i+=1&Set "ImgIn[!i!]=%"
:: Set ImgIn

set i=0&Set "ImgOut= 2 3 4 5 6 7 8 9 10 11"
Set "ImgOut=%ImgOut: ="&Set /a i+=1&Set "ImgOut[!i!]=%"
:: Set ImgOut

set i=0&Set "ImgNext= 002 003 004 005 006 007 008 009 010 011"
Set "ImgNext=%ImgNext: ="&Set /a i+=1&Set "ImgNext[!i!]=%"
:: Set ImgNext

For /L %%A IN (1,1,10) DO (
 Echo python batchLayer4ThirdVideo.py ^
 --input_image !ImgIn[%%A]! ^
 --next_undreamed_image !ImgNext[%%A]! ^
 --output_image !ImgOut[%%A]!
)

示例输出:

> Q:\Test\2018\07\05\SO_51191502.cmd
python batchLayer4ThirdVideo.py  --input_image 1  --next_undreamed_image 002  --output_image 2
python batchLayer4ThirdVideo.py  --input_image 2  --next_undreamed_image 003  --output_image 3
python batchLayer4ThirdVideo.py  --input_image 3  --next_undreamed_image 004  --output_image 4
python batchLayer4ThirdVideo.py  --input_image 4  --next_undreamed_image 005  --output_image 5
python batchLayer4ThirdVideo.py  --input_image 5  --next_undreamed_image 006  --output_image 6
python batchLayer4ThirdVideo.py  --input_image 6  --next_undreamed_image 007  --output_image 7
python batchLayer4ThirdVideo.py  --input_image 7  --next_undreamed_image 008  --output_image 8
python batchLayer4ThirdVideo.py  --input_image 8  --next_undreamed_image 009  --output_image 9
python batchLayer4ThirdVideo.py  --input_image 9  --next_undreamed_image 010  --output_image 10
python batchLayer4ThirdVideo.py  --input_image 10  --next_undreamed_image 011  --output_image 11

【讨论】:

  • 哇!我喜欢使用this technique 来创建数组元素! ;)
  • @Aacini 当然,所有的荣誉都归于你 ;-)。我现在没有那个链接,将它插入我的答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
相关资源
最近更新 更多