回声批处理文件数组使用变量作为索引? [英] Echo batch file arrays using a variable for the index?

查看:148
本文介绍了回声批处理文件数组使用变量作为索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个批处理文件,并且正在使用索引作为变量设置数组

If I have a batch file and I am setting arrays with an index that is a variable

@echo off
SET x=1
SET myVar[%x%]=happy

我该如何回应才能获得快乐"?

How do I echo that to get "happy" ?

我尝试过

ECHO %myVar[%x%]%
ECHO %%myVar[%x%]%%
ECHO myVar[%x%]

但是它们都不起作用.

如果我使用实际数字作为索引,效果很好

It works fine if I use the actual number for the index

ECHO %myVar[1]%

但如果索引号也是变量

推荐答案

SET x=1
SET myVar[%x%]=happy

call echo %%myvar[%x%]%%
set myvar[%x%]
for /f "tokens=2* delims==" %%v in ('set myvar[%x%]')  do @echo %%v
setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal

我建议您使用

setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal

因为这是表现最好的方式

as it is a best performing way

这篇关于回声批处理文件数组使用变量作为索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆