|
zhoushijay
高级用户
    Autowalk
积分 845
发帖 375
注册 2007-3-3
状态 离线
|
『楼 主』:
请教 for /l 语句中的 set /a 问题
使用 LLM 解释/回答一下
echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & echo %a%
这里显示%a% 始终都是0 似乎set /a a+=1根本不起作用,请问这是什么原因啊?是不是for /l 中不能使用 set /a ?
```
echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & echo %a%
The %a% here always shows 0. It seems that set /a a+=1 doesn't work at all. What's the reason for this? Is it that set /a can't be used in for /l?
```
|
|
2007-3-9 23:59 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
LZ没搞懂用法
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1
echo %a%
pause
如果要在同一行使用,必需要启用延迟变量(具体搜索论坛)
LZ didn't understand the usage
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1
echo %a%
pause
If you want to use it on the same line, you must enable delayed variables (specifically search the forum)
|
|
2007-3-10 00:17 |
|
|
youxi01
高级用户
   
积分 846
发帖 247
注册 2006-10-27 来自 湖南==》广东
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & call echo %%a%%
pause>nul
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1 & call echo %%a%%
pause>nul
|
|
2007-3-10 01:41 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
我也有一个不明白的地方,“set /a a+=1”,这句是啥意思啊?关键是a+=1这个用法是干什么的?哪位知道的朋友告诉我一下,谢谢了。
I also have a place I don't understand. What does "set /a a+=1" mean? The key is what the usage of a+=1 is. Which friend who knows tells me, thank you.
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-11 01:54 |
|
|
zhoushijay
高级用户
    Autowalk
积分 845
发帖 375
注册 2007-3-3
状态 离线
|
|
2007-3-11 02:00 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
谢谢楼上的这位朋友。
Thank you, friend from the upper floor.
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-11 08:19 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
   『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by oilio at 2007-3-10 12:54:
我也有一个不明白的地方,“set /a a+=1”,这句是啥意思啊?关键是a+=1这个用法是干什么的?哪位知道的朋友告诉我一下,谢谢了。
学过其它的编程语言的朋友可能一看就知道这样的用法,其它类似的用法还有很多,比如:set /a a*=2、set /a a-=5等等,分别表示set /a a=a*2、set /a a=a-5,一般把set /a a+=1成为累加器,每运行一次a的值就加1,主要用来控制程序流程的。
Originally posted by oilio at 2007-3-10 12:54:
I also have a question I don't understand. What does "set /a a+=1" mean? The key is what the usage of a+=1 is for? Friends who know, please tell me, thank you.
Friends who have learned other programming languages may know this usage at a glance. There are many similar usages, such as: set /a a*=2, set /a a-=5, etc., which respectively mean set /a a=a*2, set /a a=a-5. Generally, set /a a+=1 is called an accumulator. Each time it runs, the value of a increases by 1, which is mainly used to control the program flow.
此帖被 +4 点积分 点击查看详情 评分人:【 oilio 】 | 分数: +4 | 时间:2007-3-11 09:14 |
|
|
|
2007-3-11 08:34 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
谢谢lxmxn兄,真不知道说什么好,谢谢!这是我N次看到这个用法,因为之前一直都很弱,现在水平比以前稍微好那么一点,今天再看到这个用法觉得到时候了,忍不住想问,谢谢各位,尤其谢谢lxmxn兄列出好几个列子,lxmxn兄说到累加器,我想那这个批处理用到set /a a+=1,其实主要作用就是计算从71步长值是1递增到99,一共递增了28次,但要得到数字总的个数,次数不包括71本身,所以计算一共是多少个数字就会少一个,所以这里用到set /a a+=1, 不知道我理解的对不对?看样子今天是赚了,一下明白俩。我实践一下,加深印象去了。再次感谢各位。
Last edited by oilio on 2007-3-10 at 09:14 PM ]
Thanks, Brother lxmxn. I really don't know what to say, thanks! This is the Nth time I've seen this usage. Because I used to be very weak, now my level is a little better than before. Today, when I saw this usage again, I thought it was time, and couldn't help but want to ask. Thanks everyone, especially Brother lxmxn for listing several examples. Brother lxmxn mentioned the accumulator. I think that in this batch processing, using set /a a+=1, the main function is to calculate that from 71 with a step value of 1 increasing to 99, a total of 28 increments occur. But to get the total number of numbers, the number of times does not include 71 itself, so when calculating the total number of numbers, one is less. So here set /a a+=1 is used. I don't know if my understanding is correct? It seems that I made a gain today, and I understand two things at once. I'll practice it to deepen my impression. Thanks everyone again.
Last edited by oilio on 2007-3-10 at 09:14 PM ]
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-11 09:21 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by oilio at 2007-3-10 20:21:
谢谢lxmxn兄,真不知道说什么好,谢谢!这是我N次看到这个用法,因为之前一直都很弱,现在水平比以前稍微好那么一点,今天再看到这个用法觉得到 ...
兄不必这么客气啊,大家都在论坛里也是个缘分,共同学习共同进步嘛。
看到兄这些时的进步不小,真为兄感到高兴。
Originally posted by oilio at 2007-3-10 20:21:
Thank you, brother lxmxn. I really don't know what to say, thank you! This is the Nth time I've seen this usage. Because I've been very weak before, and now my level is a little better than before. Today, when I see this usage again, I feel that...
Brother, there's no need to be so polite. Everyone in the forum is also a fate. Let's learn and progress together.
I'm really happy to see that you've made great progress these days.
|
|
2007-3-11 09:37 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
Originally posted by oilio at 2007-3-10 20:21:
我想那这个批处理用到set /a a+=1,其实主要作用就是计算从77步长值是1递增到99,一共递增了28次,但要得到数字总的个数,次数不包括71本身,所以计算一共是多少个数字就会少一个,所以这里用到set /a a+=1, 不知道我理解的对不对?
兄的理解有点偏差,这里一共是递增了99-71+1=29次,因为 for /l 循环包含了第一个数字,这里是71,你可以用for /l %a in (1,1,29) do 来试试,结果应该是一样的。set /a a+=1 的作用并不是使其递增数加一,而是使 a 的值没循环一次就增加1,这里循环了29次,所以结果最后应该是29了。
Originally posted by oilio at 2007-3-10 20:21:
I think that the main function of using set /a a+=1 in this batch processing is to calculate that from step size 1 increasing from 77 to 99, a total of 28 increments occur, but to get the total number of numbers, the count does not include 71 itself, so when calculating the total number of numbers, it will be one less. So here set /a a+=1 is used. I don't know if my understanding is correct?
Brother's understanding is a bit deviated. Here, there are a total of 99-71+1=29 increments, because the for /l loop includes the first number, which is 71. You can try for /l %a in (1,1,29) do, and the result should be the same. The function of set /a a+=1 is not to increase the increment by one, but to make the value of a increase by 1 each time the loop is executed. Here, the loop is executed 29 times, so the final result should be 29.
|
|
2007-3-11 10:03 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
好像不对啊,我想了一下,假如是for /l %a in (1,1,3)的话,从1到2是递增了1次,从2到3是递增第二次,一共是两次才对啊,这里确实是三个数,但应该递增次数是2次吧。这个命令应该是计算从1到3的个数的吧?不知道是不是我的理解力有问题,呵呵。有的时候感觉挺聪明,有的时候感觉钻牛角尖了,嘿嘿!累了,睡觉了,我再好好想想,说不定我休息一下就能想明白,呵呵。谢谢lxxm兄的回帖,使我收获良多。
It seems incorrect. Let me think. If it's for /l %a in (1,1,3), from 1 to 2 is an increase of 1 time, and from 2 to 3 is the second increase. There are a total of 2 times. But there are actually three numbers here. Shouldn't the number of increments be 2 times? This command should be calculating the number from 1 to 3. I don't know if my understanding is wrong, heh heh. Sometimes I feel quite smart, but sometimes I get stuck, heh heh. Tired, going to sleep. I'll think about it again. Maybe I can figure it out after resting, heh heh. Thanks to brother lxxm for the reply, which has benefited me a lot.
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-11 10:13 |
|
|
zhoushijay
高级用户
    Autowalk
积分 845
发帖 375
注册 2007-3-3
状态 离线
|
|
2007-3-11 10:15 |
|
|
oilio
高级用户
    前进者
积分 641
发帖 303
注册 2007-1-10
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
谢谢,周仕杰兄,原来我的问题出在这儿,关键是我说到递增,把这个给忘了。第一个数就开始计数的话,那就对了,是我自己把自己绕进去了,哈哈。谢谢。
Thanks, Brother Zhou Shijie. So my problem was here. The key is that I mentioned incrementing and forgot about this. If I start counting from the first number, that's right. I confused myself. Haha. Thanks.
|

我相信总有一天,总会遇到一个人可以相濡以沫、相吻以湿! |
|
2007-3-11 10:19 |
|
|
6622186
高级用户
   
积分 894
发帖 411
注册 2007-2-17
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我以前也试了很多次, 不能在同一行使用, 原来用变量延迟就可以了. 具体怎么用啊?
I tried many times before, and I couldn't use it on the same line. It turns out that using variable delay can do it. How exactly to use it?
|

@set c= 不知则觉多,知则觉少,越知越多,便觉越来越少. --- 知多少.
@for,/l,%%i,in,(1,1,55)do,@call,set/p=%%c:~%%i,1%%<nul&ping/n 1 127.1>nul
|
|
2007-3-15 11:14 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
这里变量延迟有两种使用方式:
1、启用变量延迟:
@echo off
setlocal EnableDelayedExpansion
set a=0
for /l %%i in (71,1,99) do set /a a+=1&echo !a!
pause>nul
2、用call
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1&call echo %%a%%
pause>nul
Here are two ways to use variable delay:
1. Enable variable delay:
@echo off
setlocal EnableDelayedExpansion
set a=0
for /l %%i in (71,1,99) do set /a a+=1&echo !a!
pause>nul
2. Use call
@echo off
set a=0
for /l %%i in (71,1,99) do set /a a+=1&call echo %%a%%
pause>nul
|
|
2007-3-15 11:38 |
|