![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-10 15:59 |
47,811 topics / 349,897 posts / today 0 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » Ask about the `set /a` issue in the `for /l` statement |
| Printable Version 2,511 / 16 |
| Floor1 zhoushijay | Posted 2007-03-09 23:59 |
| 高级用户 Posts 375 Credits 845 | |
|
```
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? ``` |
|
| Floor2 zh159 | Posted 2007-03-10 00:17 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
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) |
|
| Floor3 youxi01 | Posted 2007-03-10 01:41 |
| 高级用户 Posts 247 Credits 846 From 湖南==》广东 | |
|
@echo off
set a=0 for /l %%i in (71,1,99) do set /a a+=1 & call echo %%a%% pause>nul |
|
| Floor4 oilio | Posted 2007-03-11 01:54 |
| 高级用户 Posts 303 Credits 641 | |
|
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.
|
|
| Floor5 zhoushijay | Posted 2007-03-11 02:00 |
| 高级用户 Posts 375 Credits 845 | |
|
The `set /a a+=1` is equivalent to `set /a a=%a%+1` in the context of batch scripting in Windows. So the translation is: `set /a a+=1` means equal to `set /a a=%a%+1`
|
|
| Floor6 oilio | Posted 2007-03-11 08:19 |
| 高级用户 Posts 303 Credits 641 | |
|
Thank you, friend from the upper floor.
|
|
| Floor7 lxmxn | Posted 2007-03-11 08:34 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by oilio at 2007-3-10 12:54: 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. |
|
| Floor8 oilio | Posted 2007-03-11 09:21 |
| 高级用户 Posts 303 Credits 641 | |
|
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 ] |
|
| Floor9 lxmxn | Posted 2007-03-11 09:37 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by oilio at 2007-3-10 20:21: 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. |
|
| Floor10 lxmxn | Posted 2007-03-11 10:03 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by oilio at 2007-3-10 20:21: 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. |
|
| Floor11 oilio | Posted 2007-03-11 10:13 |
| 高级用户 Posts 303 Credits 641 | |
|
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.
|
|
| Floor12 zhoushijay | Posted 2007-03-11 10:15 |
| 高级用户 Posts 375 Credits 845 | |
|
for /l %a in (1,1,3) do
When %a=1, count once When %a=2, count once When %a=3, count once So it should be 3 times |
|
| Floor13 oilio | Posted 2007-03-11 10:19 |
| 高级用户 Posts 303 Credits 641 | |
|
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.
|
|
| Floor14 6622186 | Posted 2007-03-15 11:14 |
| 高级用户 Posts 411 Credits 894 | |
|
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?
|
|
| Floor15 zh159 | Posted 2007-03-15 11:38 |
| 金牌会员 Posts 1,467 Credits 3,687 | |
|
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 |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |