Hoffs
Hoffs

Reputation: 127

raising token variable inside double for loop

So I have this piece of code which is basically just a prototype, but I can't get it to work because tokens parameter of for loop doesn't like variables defined in a !var! way which can be used with delayed expansion.

@echo on
setlocal enabledelayedexpansion
set string=4 1 5 1 6 1 6 2
set b=1
set c=2
for /L %%o in (1, 2, 8) do (
  call set /a "b=%%o"
  call set /a "c=%%o + 1"
  FOR /F "tokens=%b%,%c%" %%G IN ("%string%") DO @echo %%G %%H
)

The result that I am getting is:

4 1
4 1
4 1
4 1

And the result I want is:

4 1
5 1
6 1
6 1

I have been experimenting for a while how to fix this issue but couldn't find a solution anyways. Is there any simple way to fix this or do I have to try and make it with single loop and maybe some goto's or call functions?

Upvotes: 0

Views: 24

Answers (1)

Squashman
Squashman

Reputation: 14290

Just call out to a function.

@echo off
setlocal enabledelayedexpansion
set string=4 1 5 1 6 1 6 2
set b=1
set c=2
for /L %%o in (1, 2, 8) do (
  set /a "b=%%o"
  set /a "c=%%o + 1"
  CALL :label1 !b! !c!
)
PAUSE
GOTO :EOF


:label1
FOR /F "tokens=%1,%2" %%G IN ("%string%") DO @echo %%G %%H
GOTO :EOF

Upvotes: 1

Related Questions