Strange for loop behaviour

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Strange for loop behaviour

#1 Post by penpen » 19 Apr 2018 12:20

I've played around with my colormap sample code in the latest version from IcarusLives, when i noticed a strange behaviour (on my windows 10, x64 - actually i cannot test anywhere else):
The variable i doesn't get updated properly in each loop step.

Here is a shortened example (first part) and maybe (i'm unsure) a hint to what codepart causes that issue (second part; removed the "set... & part"):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

set "loadColors=(set "i=0" & (for %%a in (!table!) do (set "c!i!=%%~a" & set /a "i+=1")))"
set loadColors

set /A "x=0, i=0"
set "timestamp="
for /l %%b in (1,1,3000) do if not "!timestamp!" == "!time!" (
	set /A "i=(i+1) %% 4, x+=((i-1)>>31)&1"
	echo(!i!, !x!

	%loadcolors:table=colorTable1%

	set "timestamp=!time!"
)
pause

set "loadColors=( (for %%a in (!table!) do (set "c!i!=%%~a" & set /a "i+=1")))"
set loadColors

set /A "x=0, i=0"
set "timestamp="
for /l %%b in (1,1,3000) do if not "!timestamp!" == "!time!" (
	set /A "i=(i+1) %% 4, x+=((i-1)>>31)&1"
	echo(!i!, !x!

	%loadcolors%

	set "timestamp=!time!"
)
pause

goto :eof
Output:

Code: Select all

loadColors=(set "i=0" & (for %a in () do (set "c=%~a" & set /a "i+=1")))
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
1, 0
Drücken Sie eine beliebige Taste . . .
loadColors=( (for %a in () do (set "c0=%~a" & set /a "i+=1")))
1, 0
2, 0
3, 0
0, 1
1, 1
2, 1
3, 1
0, 2
1, 2
2, 2
3, 2
0, 3
1, 3
2, 3
Drücken Sie eine beliebige Taste . . .
I really don't understand why that issue occurs!
(Hope it's not my fever preventing me from finding a solution.)


penpen

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Strange for loop behaviour

#2 Post by jeb » 19 Apr 2018 13:15

Hi penpen,

I'm a bit puzzeled about what result you expect. :?:

I'm not really surprised, that you always get i=1

I simplyfy your code a bit, trying to build a minimal example.
I first removed the timestamp stuff and the variable x and also the %% 4

Code: Select all

set "loadColors=(set "i=0" & (for %%a in (!table!) do (set "c!i!=%%~a" & set /a "i+=1")))"
set loadColors

set /A "i=0"
for /l %%b in (1,1,30) do  (
	set /A "i=(i+1)
	echo(!i!
	%loadcolors:table=colorTable1%
)
pause
In the next step I move the expanded loadcolor macro into the loop

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

set /A "i=0"
for /l %%b in (1,1,30) do  (
	set /A "i=(i+1)"
	echo(!i!
	(set "i=0" & (for %%a in () do (set "c=%%~a" & set /a "i+=1")))
)
In the last step I remove the useless inner loop, as the parameter is empty.

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

set /A "i=0"
for /l %%b in (1,1,30) do  (
	set /A "i=(i+1)"
	echo(!i!
	(set "i=0")
)
1. Before the loop you set i=0
2. in the loop i=i+1, in this case this is i=1
3. echo !i!
4. you set i=0
5. go to step 2

I'm not sure what other results you are expecting from this code :D

Probably you missed the point, that your macro expansion doesn't work properly, as "table" will never be replaced by "colorTable1", as "table" is already removed in your definition.
Perhaps you want to try

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "loadColors=( (for %%a in (^!table^!) do (set "c^^!i^^!=%%~a" & set /a "i+=1")))"

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Strange for loop behaviour

#3 Post by pieh-ejdsch » 19 Apr 2018 13:30

the macro "loadColors" is created within delayedexpansion.
As a result, the variable table and i - are non-existent (or even set to an undefined content) and will/can not be replaced in the macro.
The line to represent the variable loadColors proves this fact.

in the second attempt the variable i is set to 0.
I do not know what is in table, but it will probably make the calculation differently.
The variable "loadColors" must under all circumstances be created in a disable delayedexpansion.
otherwise the macro will not work.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Strange for loop behaviour

#4 Post by penpen » 19 Apr 2018 13:43

Oh :oops: , Sometimes i'm feeling really dumb...
i've seen the two "i" variables as different variables... .
I have to remove the access i in the colortable.

Thanks jeb!


@ pieh-ejdsch: Thanks to you, too.
I used the above to set colors within a colortable, so that is planned; see:
viewtopic.php?f=3&t=8493&start=15#p56409.
Edit: And in the original i defined that variable in disabled delayed expansion although it was a big flaw.


penpen

Post Reply