|
hongewuyan
初级用户
 
积分 65
发帖 29
注册 2008-3-24
状态 离线
|
『楼 主』:
[已结]一个变量嵌套变量的问题
使用 LLM 解释/回答一下
请教一个问题,有一个功能一直不知道如何实现.
举例如下:
先设置下面两个变量:
set a=1
set b1=10
然后执行 echo %b%a%% 得到 %b1%
其实我想得到的是 赋予b1的值,即 10
请问大家能否实现呢?
Last edited by HAT on 2008-12-23 at 10:10 ]
Ask a question, there is a function I have always not known how to implement.
For example:
First set the following two variables:
set a=1
set b1=10
Then execute echo %b%a%% to get %b1%
In fact, what I want to get is the value assigned to b1, that is, 10
Ask everyone, can it be achieved?
Last edited by HAT on 2008-12-23 at 10:10 ]
|
|
2008-6-23 17:02 |
|
|
bat-zw
金牌会员
      永远的学习者
积分 3105
发帖 1276
注册 2008-3-8
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
```
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
```
|

批处理之家新域名:www.bathome.net |
|
2008-6-23 17:15 |
|
|
hongewuyan
初级用户
 
积分 65
发帖 29
注册 2008-3-24
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
果然可以实现
谢谢楼上的高手.
不过还是有些疑问
call,echo %%b%a%%%
这行可以解释下么? call, 做何解?
我直接 echo %%b%a%%% 就不可以,加上就可以了...
call不是调用其他批处理或标签的么...而且后面那个逗号也不懂 - -
It turns out it can be achieved. Thanks to the expert upstairs. But there are still some doubts. What does "call" mean in the line "call,echo %%b%a%%%"? I can't do it directly by echoing "%%b%a%%%", but it works... I know that "call" is used to call other batch files or labels, and the comma behind it is confusing.
|
|
2008-6-23 17:33 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by hongewuyan at 2008-6-23 17:33:
果然可以实现
谢谢楼上的高手.
不过还是有些疑问
call,echo %%b%a%%%
这行可以解释下么? call, 做何解?
我直接 echo %%b%a%%% 就不可以,加上就可以了. ...
call 这里实际是对命令行进行重新组织扩展,先扩展%%b%a%%%里面的%a%,使%a%变成a的值1,再用cal来扩展%b1%。
也可以用变量延迟来实现,方法如下:
@echo off
set /a a=1,b1=10
Setlocal EnableDelayedExpansion
echo:!b%a%! ...
pause
call 在这里的用法实际是变量延迟的一种快捷方式,变量延迟一般用在for的循环体里面。
call,%%b%a%%% 这里的逗号实际是一个分隔符,和空格一样,还有很多分隔符可用,比如上例中的 echo:!b%a%! ,当然并不是所有的命令都可以这样用,看情况而定…… C:\>echo+cn-dos.net
cn-dos.net
C:\>echo/cn-dos.net
cn-dos.net
C:\>echo
Originally posted by hongewuyan at 2008-6-23 17:33:
It really works.
Thanks to the expert above.
But there are still some doubts.
call,echo %%b%a%%%
Can you explain this line? What does call mean?
I can't do it directly with echo %%b%a%%%, but it works when adding call. ...
Here, call actually reorganizes and expands the command line. First, expand %a% inside %%b%a%%% so that %a% becomes the value 1 of a, and then use cal to expand %b1%.
It can also be implemented using variable delay. The method is as follows:
@echo off
set /a a=1,b1=10
Setlocal EnableDelayedExpansion
echo:!b%a%! ...
pause
The usage of call here is actually a quick way of variable delay. Variable delay is generally used in the loop body of for.
In call,%%b%a%%%, the comma here is actually a separator, the same as a space. There are many separators available. For example, in the above example, echo:!b%a%! ,Of course, not all commands can be used in this way, depending on the situation... C:\>echo+cn-dos.net
cn-dos.net
C:\>echo/cn-dos.net
cn-dos.net
C:\>echo
|
|
2008-6-23 22:07 |
|
|
pusofalse
银牌会员
    
积分 1604
发帖 646
注册 2008-4-13
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
我的理解是,如果变量之中仍有变量,就要用到变量延迟 call 延迟 或setlocal 开启延迟 不知这样理解对否。。。
My understanding is that if there are still variables within variables, you need to use variable delay call delay or setlocal to enable delay. I wonder if this understanding is correct...
|

心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^ |
|
2008-6-23 22:17 |
|
|
hongewuyan
初级用户
 
积分 65
发帖 29
注册 2008-3-24
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
谢谢大家的帮助,这次真的明白了。。。
Thanks everyone for your help, I really understand this time...
|
|
2008-6-24 11:24 |
|
|
mysuntjy
新手上路

积分 13
发帖 10
注册 2008-11-13 来自 重庆市
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by bat-zw at 2008-6-23 17:15:
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
为什么将连接两个set语句的&两边加个空格,即:set a=1 & set b1=10,运行结果为:ECHO 处于关闭状态.
????
Originally posted by bat-zw at 2008-6-23 17:15:
@echo off
set a=1&set b1=10
call,echo %%b%a%%%
pause>nul
Why does adding a space on both sides of the & connecting the two set statements, that is: set a=1 & set b1=10, result in "ECHO is off"?
|
|
2008-12-23 07:13 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
Originally posted by mysuntjy at 2008-12-23 07:13:
为什么将连接两个set语句的&两边加个空格,即:set a=1 & set b1=10,运行结果为:ECHO 处于关闭状态.
????
加了空格,变量a的值就是1加上一个空格了,而不是原来单独的一个1。
Originally posted by mysuntjy at 2008-12-23 07:13:
Why does adding a space on both sides of & connecting two set statements, that is: set a=1 & set b1=10, the running result is: ECHO is off.
????
After adding the space, the value of variable a is 1 plus a space, not the original single 1.
|
|
2008-12-23 09:48 |
|
|
95167958
新手上路

积分 1
发帖 1
注册 2008-12-22
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
call,echo %%b%a%%%语句中为什么不是call,echo %b%a%%,而要多出两个%呢,请指教
In the statement `call,echo %%b%a%%`, the extra `%` signs are related to how batch processing in Windows handles variable expansion. In a batch file, when you want to literally output a `%` character, you need to use `%%` because the single `%` is used for variable substitution. Let's break it down:
The part `%%b%a%%` - the first `%%` is to output a single `%` before the variable `b`, then `%a%` is the variable `a` with the surrounding `%` signs, and the last `%%` is to output a single `%` after. If you just wrote `call,echo %b%a%%`, there would be issues with how the batch parser interprets the variable expansion and the literal `%` characters. So the extra `%` signs are there to correctly handle the literal `%` output and proper variable expansion in the batch processing context.
|
|
2008-12-23 12:23 |
|
|
HAT
版主
       
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
|
2008-12-23 13:14 |
|
|
gool123456
初级用户
 
积分 89
发帖 76
注册 2009-12-13
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
哈哈,恍然大悟。
好帖不能沉,顶上让多点人看到!
Haha, suddenly I see the light.
A good post shouldn't sink, top it to let more people see!
|

Discuz! 现在时间 |
|
2010-5-27 17:28 |
|