使用EnableDelayedExpansion的嵌套变量名称 [英] Nested variable name using EnableDelayedExpansion

查看:99
本文介绍了使用EnableDelayedExpansion的嵌套变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用脚本来获取每列的最大长度,我正在尝试将最大长度的长度存储在_c1 ... n vars中.列数未知.

I am working on a script to get max lengths of each column, I'm trying to store lengths of max length in _c1...n vars. number of columns unknown.

我能够获得每一列的长度,创建变量来存储每个带有_c!i!的变量! =!n!,n是长度 但是为了设置特定列的最大长度,我需要将电流与max进行比较,并使用!_c !! i !!哪个不起作用,有什么主意如何引用一个变量,该变量的名称的一部分来自另一个变量?

I was able to get length for each column, create variables to store each with set _c!i! = !n!, n is the length but in order to set the max length for a particular column I need to compare current with max and use something like !_c!!i!! which doesn't work, any ideas how to refer a variable which part of it's name coming from another variable?

谢谢...

推荐答案

我假设您使用的是延迟扩展字符,因为您正在一组括号()"内工作.这样做会使您的过程更加困难.我知道该方法更易于阅读,但很难为其编写代码.

I assume that you are using the delayed expansion character because you are working inside a set of brackets "()". Doing that makes your process harder. I know that method is easier to read, but it is harder to code for.

在方括号内,我只知道一种方法来访问由一个或多个变量构建"的变量.那就是使用call函数使汇编变量激活".此方法在括号内和括号内均有效.

Inside brackets, I know of only one method to access a variable that was 'built' out of one or more variables. That is to use the call function to cause the assembled variable to 'activate'. This method works both inside and outside of brackets.

这是一个小例子:

@echo off
setlocal enabledelayedexpansion
(
  set i=10
  set _c!i!=something
  :: below is equivalent to echo !_c10!
  call echo %%_c!i!%%
)
endlocal

输出:

something

您几乎可以使用它前面的CALL来完成所有操作,尽管在XP或更早的版本中,您不能调用诸如if之类的内部命令,而只能调用诸如FIND.EXE之类的外部"程序.

You can do almost everything using a CALL in front of it that you can without it, though in XP or earlier you cannot call internal commands like if and can only call 'external' programs like FIND.EXE.

如果可以使用call :label语句在一组括号之外工作,则可以像这样简单地访问变量:

If you can work outside of a set of brackets by possibly using a call :label statement, you can simply access the variable like this:

@echo off
setlocal enabledelayedexpansion
set i=10
set _c!i!=something

:: The below 2 statements are equivalent to `echo %_c10%`
echo !_c%i%!
call echo %%_c!i!%%

endlocal

输出:

something
something

这篇关于使用EnableDelayedExpansion的嵌套变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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