I'm trying to find a way to make nested for loops work, but this iteration is different than the most popular results (where an OP is looping through directories, or using a numerical for /l loop, etc.)
Instead, I'm trying to figure out how to make this work:
@echo Off
setlocal enabledelayedexpansion enableextensions
for /f "Tokens=1-7 Delims=_" %%P in ("Testing_This_GREAT_Thing_Of_An_IDEA") do (
Echo %%P
For %%a in (P Q R S T U V ) do (
Call set "term=%%%%%%a"
Call echo !term!
Call set term=%%term%%
Call Echo !term!
Call set term=%%term%%
Call Echo !term!
If not "!term!"=="" Call set word.%%a=%%term%%
Echo word.%%a = "!word.%%a!"
)
)
pause
exit /b
Desired output of For %%a in (P Q R S T U V) loop would be to have:
word.P=Testing
word.Q=This
word.R=GREAT
word.S=Thing
word.T=Of
word.U=An
word.V=IDEA
Obviously the following would be as expected for the initial loop, but I cannot get the delayed expansion (I assume) to work as expected. . . .
%%P=Testing
%%Q=This
%%R=GREAT
%%S=Thing
%%T=Of
%%U=An
%%V=IDEA
No, it's not possible (in a single block).
You try to dynamically access a FOR meta variable, but FOR meta variables are recognized only in the parsing phase, before the code block is executed.
An inline call can't help here, because a FOR meta variable isn't detected at all in the second parsing round of a call command.
But you could use a helper function, using the fact that you can access all FOR meta variables in any FOR block, even when they are not in the same block.
@echo Off
setlocal enabledelayedexpansion enableextensions
for /f "Tokens=1-7 Delims=_" %%P in ("Testing_This_GREAT_Thing_Of_An_IDEA") do (
Echo %%P
For %%a in (P Q R S T U V ) do (
Call :read_meta_var term %%a
If not "!term!"=="" Call set word.%%a=%%term%%
Echo word.%%a = "!word.%%a!"
)
)
pause
exit /b
:read_meta_var
REM *** %1=Variable to store the result
REM *** %2=Character of the meta variable to read
for %%d in ("dummy") do (
set "%1=%%%2"
)
Adding another answer I stumbled upon which operates on the premise presented by @T3RR0R's Comment to my original post - @T3RR0R Linked to this article at dostips.com, where we are presented with the idea of parsing substrings from one string using whatever delimiter we want.
Modifying one of the examples set within this article, we can arrive at the following which uses a zero-indexed "array" like we're used to seeing in almost all other programming languages.
set "x=Testing_This_GREAT_Thing_Of_An_IDEA"
set i=0
set "x.!i!=%x:_=" & set /a i+=1 & set "x.!i!=%"
REM output x var's:
set x.
Which yields the fantastically succinct output of:
x.0=Testing
x.1=This
x.2=GREAT
x.3=Thing
x.4=Of
x.5=An
x.6=IDEA
I played around with trying to set "x.a"-"x.g" using variable positioning and the delayed expansion of i where it is asserted that "verb=abcdefghijklmnopqrstuvwxyz", by using:
set "x.!y!=%x:_=" & set /a i+=1 & Call set "y=%%verb:~!i!,1%%" & set "x.!y!=%"
Unfortunately, it always ended with x.a=Testing and nothing else defined, while y=IDEAverb:~7,1 always occurred, boggling my mind. . . I would love to get this to work in a one-liner!
Fortunately, if I take a For /l numerical loop, I can accomplish my goal in 2 lines with the assertion that x and verb are already set previously as described using the original substitution line:
set "x.!i!=%x:_=" & set /a i+=1 & set "x.!i!=%"
For /l %%l in (0,1,!i!) do (For /f "Tokens=1" %%q in ("!verb:~%%l,1!") do ( set "word.%%q=!x.%%l!" ) )
If it is possible to perform this variable substitution/positioning in one line using the calculation to i in one line, this would be fantastic, otherwise, for the sake of sanity, an extra line is not a problem for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With