英文:
Expand variable in a batch file For loop
问题
Sure, here's the translated code:
我想显示一个由提示的变量组成的数组的内容。
所以,我编写了以下代码:
::创建一个数组
SET country[0]=阿尔巴尼亚
SET country[1]=阿根廷
SET country[2]=澳大利亚
SET country[3]=奥地利
SET country[4]=比利时
::创建一个菜单
SET /P C=输入您的国家的数字,然后按回车键:
SET /A C_num=%C%
ECHO.
for /L %%a in (0, 1, 59) do call ECHO %%a - %%country[%%a]%%
ECHO.
以及我尝试使用以下代码显示相应的国家:
ECHO %country[!C_num!]%
但是它没有成功显示内容。您能帮我修复吗?
英文:
I want to display the content of an array with a prompted variable.
So, I make the following code:
::Create an array
SET country[0]=Albania
SET country[1]=Argentina
SET country[2]=Australia
SET country[3]=Austria
SET country[4]=Belgium
::Create a menu
SET /P C=Type the number of your country, then press ENTER:
SET /A C_num=%C%
ECHO.
for /L %%a in (0, 1, 59) do call ECHO %%a - %%country[%%a]%%
ECHO.
And I tried this code to display the corresponding country:
ECHO %country[!C_num!]%
However it didn't succeed in displaying the content.
Can you help me to fix it?
答案1
得分: 1
Sure, here's the translated content:
嗯...说实话,如果你成功了,你就不需要问这个问题了...
所以 - 要解决你的问题,我们需要了解更多情况。首先是关于你的文件是如何开始的。批处理文件的正常开头是:
@echo off
setlocal
这会关闭默认的命令回显,以防止屏幕上杂乱的输出,并建立一个“本地环境”,具有一个有用的属性,即当批处理结束时,运行批处理时修改的所有变量将恢复到它们在批处理启动时的原始值(批处理启动时的值)。这可以防止在同一会话中运行的后续批处理继承此批处理所做的修改。
下一个问题是你的set
语句的格式。对于设置字符串值,请使用set "var=value"
- 这可以避免由尾随空格引起的问题。不要分配"
或终端反斜杠杠或空格。从元素构建路径名可能会让过程变得更加直观,这可能会让过程更加容易。如果使用语法set var="value"
,那么引号将成为分配的值的一部分。
还有一个小技巧:如果按<kbd>Enter</kbd>键,SET /P "var=Prompt"
不会更改var
。因此,如果var
最初是空的,它将保持为空。使用如上所示的引号,您可以在Prompt
的末尾添加一个空格。
我建议在要求进行选择之前最好先显示列表 - 这可能会使它变得更容易一些。
您有一个(潜在的)很长的列表要显示,其中一些条目为空。您可以修改您的%%a
循环为...do if defined country[%%a] echo ...
,这可能会通过不显示任何未定义的country[?]
来缩短显示的列表。
我更喜欢避免使用ADFNPSTXZ(在任何情况下都是如此)作为元变量(循环控制变量)。ADFNPSTXZ也是可以导致难以找到的错误的元变量修饰符(请参阅提示中的for/f
文档)。
批处理中的变量始终是字符串。可用的数学函数会将变量内容转换为数值以执行计算,然后再次转换为字符串以进行赋值或echo
。因此,“将”C
转换为C_num
实际上没有任何作用。country[%C%]
与country[%C_num%]
完全相同。
您没有告诉我们在尝试ECHO %country[!C_num!]%
时得到了什么结果。您需要对延迟扩展有透彻的了解。
因此 - 您需要首先调用delayedexpansion
,然后使用ECHO !country[%C_num%]!
。
另一方面,您可以尝试适应这个:带有3列的菜单,它使用choice
- 这是首选的,因为它消除了对潜在随机用户输入的处理。
英文:
Hmm... well, TBH, you wouldn't need to ask the question if you did succeed...
So - to solve your problem, we'd need to know a little more The first is about how your file starts. The normal start to a batch file is
@echo off
setlocal
which turns default command-echoing off to not clutter the screen and then establishes a "local environment" which has the useful property that when the batch ends, all variables modified when running the batch are restored to their original values (those they had when the batch started). This prevents a subsequent batch that is run in the same session from inheriting the modifications made by this batch.
Next issue is the format of your set
statements. Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign "
or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value"
is used, then the quotes become part of the value assigned.
And a little quirk: SET /P "var=Prompt"
does not change var
if <kbd>Enter</kbd> alone is pressed. Consequently, if var
is originally empty, it remains empty. With the quotes as shown, you can put a space at the end of Prompt
.
I'd suggest it would be better if you displayed your list before you ask for a selection - it might just make it a little easier.
You have (potentially) a great long list to display, some entries in which are empty. You could modify your %%a
loop to …do if defined country[%%a] echo …
which may shorten the dispayed list by not showing any country[?]
that has not been defined.
I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables)
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs
(See `for/f` from the prompt for documentation)
Variables in batch are always strings. The mathematical functions available convert the variable contents to numerics to perform the calculations, then back again to strings to be assigned or echo
ed. Consequently, "converting" C
to C_num
actually achieves nothing. country[%C%]
is exactly the same as country[%C_num%]
.
You haven't told us what you get when you try ECHO %country[!C_num!]%
. You need a thorough understanding of Delayed expansion
So - you need to first invoke delayedexpansion
and then ECHO !country[%C_num%]!
On the other hand, you could try adapting this: Menu with 3 columns which uses choice
- preferred because it obviates processing of potential random user-input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论