jl66
asked on
How to get right value in loop in DOS shell script
Would like to get the right value in this script:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N% ) do (
@echo %var%
if %%i lss 3 (
set %var%=B
) else (
if %%i lss 6 (
set %var%=C
) else (
set %var%=D
)
)
)
Expecting the output as follows
A
B
C
C
C
D
D
D
D
D
Actually not. Can any gurus help me out?
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N% ) do (
@echo %var%
if %%i lss 3 (
set %var%=B
) else (
if %%i lss 6 (
set %var%=C
) else (
set %var%=D
)
)
)
Expecting the output as follows
A
B
C
C
C
D
D
D
D
D
Actually not. Can any gurus help me out?
If you want to pre-set var to A and place ECHO at the start of the loop then you will need this:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N%) do (
echo !var!
if %%i lss 5 (
if %%i lss 2 (
set var=B
) else (
set var=C
)
) else (
set var=D
)
)
exit /b
Are you sure you want:
A B CCC DDDDD
instead of:
A BB CCC DDDD
?
A B CCC DDDDD
instead of:
A BB CCC DDDD
?
Similarly, you could place the ECHO at the end of the loop (as in the first program above) and pre-set var to A like this:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N%) do (
if %%i lss 6 (
if %%i lss 3 (
if %%i gtr 1 (
set var=B
)
) else (
set var=C
)
) else (
set var=D
)
echo !var!
)
Here are two other methods (pre-processing and post-processing var):
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N%) do (
echo !var!
if %%i equ 1 set var=B
if %%i equ 2 set var=C
if %%i equ 5 set var=D
)
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=10
set var=A
for /l %%i in (1,1,%N%) do (
if %%i equ 2 set var=B
if %%i equ 3 set var=C
if %%i equ 6 set var=D
echo !var!
)
This is another way you can do it:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set N=9
set var=ABCCCDDDDD
for /l %%i in (0,1,%N%) do (
echo !var:~%%i,1!
)
Finally, here's the shortest way of doing it:
Or like this:
@for %%i in (A B C C C D D D D D) do @echo %%i
Yep! Just a one-liner!Or like this:
@echo off
set var=A B C C C D D D D D
for %%i in (%var%) do echo %%i
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
gerwinjansen
I think it was more challenging doing "A B CCC DDDD" which now leads me to think this may have in fact been a classroom exercise.
jl66 specified the following output:
A
B
C
C
C
D
D
D
D
D
I queried this in http:#37790053 however, there has been no feedback suggesting otherwise.
I think it was more challenging doing "A B CCC DDDD" which now leads me to think this may have in fact been a classroom exercise.
jl66 specified the following output:
A
B
C
C
C
D
D
D
D
D
I queried this in http:#37790053 however, there has been no feedback suggesting otherwise.
ASKER
Thanks a lot for everyone. Paul really made a big effort. Qlemo points out the key: I forgot the expression of the variable in loop: !xxx!.
No, this is not a classroom homework!
No, this is not a classroom homework!
jl66
It wouldn't have mattered even if it were a classroom exercise because you had done nearly all the work yourself anyway.
Qlemo did well to point out your wrong use of 'set %var%=value'.
Of course, the other problem relates to using '%var%' instead of '!var!' however, this was not explicitly pointed out to you - you likely gathered this looking at solutions. Apologies for not pointing out these two obvious issues from the onset.
It wouldn't have mattered even if it were a classroom exercise because you had done nearly all the work yourself anyway.
Qlemo did well to point out your wrong use of 'set %var%=value'.
Of course, the other problem relates to using '%var%' instead of '!var!' however, this was not explicitly pointed out to you - you likely gathered this looking at solutions. Apologies for not pointing out these two obvious issues from the onset.
@paul, when looking at the original code, the output specified by OP is wrong, imho, my output is right :-) Anyway, OP learned some here...
gerwinjansen
You're probably right. Maybe I was reading more into the question than there really was.
jl66
Perhapss you would like to confirm which output you intended; column 1 or column 2:
You're probably right. Maybe I was reading more into the question than there really was.
jl66
Perhapss you would like to confirm which output you intended; column 1 or column 2:
Column 1 Column 2
A A
B B
C B
C C
C C
D C
D D
D D
D D
D D
ASKER
Not compromise anything. Both are right. The point is that as mentioned before I forgot !var! in loop. Thank all of you so much.
Open in new window