Variable nesting, odd behavior maybe useful. Update: not new, already handled at DosTips
Posted: 03 Feb 2022 15:34
I wanted a routine for expanding nested variables no matter how many levels deep.
I was fiddling with dynamically constructing FOR statements for each nested level to expand nested variables when i stumbled upon some interesting behavior, and an error but that was expected and easily resolved.
Below is the code to reproduce the results.
This produces:
Normal echo: !test8! 15
Expanded echo: !test7! 14 15
Label FOR echo: !test5! 12 13 14 15
End
All expected.
When adjusting routine ResolveNested:
Notice there is no code after the second FOR statement which obviously states an error:
Normal echo: !test8! 15
Expanded echo: !test7! 14 15
) was unexpected at this time.
Also, totally expected.
Now comes the odd behavior, just some code like a simple REM statement in the FOR block:
and running it again IN THE SAME CONSOLE WINDOW gives the following results:
Normal echo: a value 123 456 789 10 11 12 13 14 15
Expanded echo: a value 123 456 789 10 11 12 13 14 15
Resolving variables...
Label FOR echo: a value 123 456 789 10 11 12 13 14 15
End
All variables are completely resolved! Didn't expect that.
It seems, when stumbling upon such error, the command parser resolves all variables before exit.
Though the FOR statement without inner code must be placed inside a routine for this behavior to work as far as i know for now.
I wonder about the mechanism behind this and if this can be used somehow.
I haven't come accross this in the forum so i thought to mention it.
Currently investigating the caveats and possibilities of implementation.
Edit: just a note to emphasize that the error has to occur to reproduce the results as far as i know for now.
Edit: some typos.
I was fiddling with dynamically constructing FOR statements for each nested level to expand nested variables when i stumbled upon some interesting behavior, and an error but that was expected and easily resolved.
Below is the code to reproduce the results.
Code: Select all
REM Case study: "variable refences variables references variable..."
set "test1=a value 123"
set "test2=!test1! 456"
set "test3=!test2! 789"
set "test4=!test3! 10"
set "test5=!test4! 11"
set "test6=!test5! 12"
set "test7=!test6! 13"
set "test8=!test7! 14"
set "test9=!test8! 15"
echo(Normal echo: %test9%
setlocal enableDelayedExpansion
echo(Expanded echo: %test9%
call:ResolveNested "%test9%"
endlocal
echo(End
exit /b
:ResolveNested
for /f "tokens=*" %%b in ("%1") do (
echo(Label FOR echo: %%~b
)
exit /b
Normal echo: !test8! 15
Expanded echo: !test7! 14 15
Label FOR echo: !test5! 12 13 14 15
End
All expected.
When adjusting routine ResolveNested:
Code: Select all
:ResolveNested
for /f "tokens=*" %%b in ("%1") do (
echo(Resolving variables...
for /f "tokens=*" %%c in ("%b") do (
)
echo(Label FOR echo: %%~b
)
exit /b
Normal echo: !test8! 15
Expanded echo: !test7! 14 15
) was unexpected at this time.
Also, totally expected.
Now comes the odd behavior, just some code like a simple REM statement in the FOR block:
Code: Select all
:ResolveNested
for /f "tokens=*" %%b in ("%1") do (
echo(Resolving variables...
for /f "tokens=*" %%c in ("%b") do (
REM
)
echo(Label FOR echo: %%~b
)
exit /b
Normal echo: a value 123 456 789 10 11 12 13 14 15
Expanded echo: a value 123 456 789 10 11 12 13 14 15
Resolving variables...
Label FOR echo: a value 123 456 789 10 11 12 13 14 15
End
All variables are completely resolved! Didn't expect that.
It seems, when stumbling upon such error, the command parser resolves all variables before exit.
Though the FOR statement without inner code must be placed inside a routine for this behavior to work as far as i know for now.
I wonder about the mechanism behind this and if this can be used somehow.
I haven't come accross this in the forum so i thought to mention it.
Currently investigating the caveats and possibilities of implementation.
Edit: just a note to emphasize that the error has to occur to reproduce the results as far as i know for now.
Edit: some typos.