Solved delayed expansion and substring processing?

Dell / LATITUDE D610
March 22, 2012 at 15:17:22
Specs: Microsoft Windows Server 2003 R2 SP2, 2.01GHz / 1GB
Hi,
I'd like to create variable names where I can control the sort order of the values that the variables contain. I had hoped the following would facilitate such a desire:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A position=0
SET order=DEFABC

FOR /L %%A IN (1,1,6) DO (
    SET sequence=%order:~!position!,1%
    SET some_variable_!sequence!=%%A
    SET /A position+=1
)

FOR /F "tokens=1* delims==" %%A IN ('SET some_variable_') DO (
    ECHO "%%A" = "%%B"
)

EXIT /B

Desired output:

"some_variable_A" = "4"
"some_variable_B" = "5"
"some_variable_C" = "6"
"some_variable_D" = "1"
"some_variable_E" = "2"
"some_variable_F" = "3"

Actual output:

"some_variable_order:~0,1" = "1"
"some_variable_order:~1,1" = "2"
"some_variable_order:~2,1" = "3"
"some_variable_order:~3,1" = "4"
"some_variable_order:~4,1" = "5"
"some_variable_order:~5,1" = "6"

What am I missing? You folks here are pretty sharp, and I was curious if there is an alternate method that would accomplish my goal.

*Edit - Fixed typo
When your only tool is a hammer, every problem looks like a nail.


See More: delayed expansion and substring processing?

Report •

✔ Best Answer
March 23, 2012 at 19:34:57
You don't even need to use call:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A position=0
SET order=DEFABC

FOR /L %%A IN (1,1,6) DO (
    for %%b in (!position!) do SET sequence=!order:~%%b,1!
    SET some_variable_!sequence!=%%A
    SET /A position+=1
)

FOR /F "tokens=1* delims==" %%A IN ('SET some_variable_') DO (
    ECHO "%%A" = "%%B"
)

EXIT /B




#1
March 23, 2012 at 09:31:27
When you code nested variables using delayed expansion you need to embrace the external one with !, i.e.
replace
set sequence=%order:~!position!,1%
with
set sequence=!order:~%position%,1!


Report •

#2
March 23, 2012 at 10:34:43
Aye. As the name delayedExpansion implies, the % tokens are evaluated before the ! tokens.

How To Ask Questions The Smart Way


Report •

#3
March 23, 2012 at 15:59:25
Thanks for the replies!

Unfortunately, %position% remains unchanged, retaining the value of zero.

(New) actual output after modification:

"some_variable_D" = "6"

I figured since %order% is for lack of a better term a "static" variable and !position! is the "dynamic" variable, the original construct to be more appropriate. I guess the crux of the problem is that "!sequence!" is not being interpreted as %order:~0,1% as I would expect, allowing for the desired substring selection.

When your only tool is a hammer, every problem looks like a nail.


Report •

Related Solutions

#4
March 23, 2012 at 19:28:29
Found it with an additional CALL:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A position=0
SET order=DEFABC

FOR /L %%A IN (1,1,6) DO (
    CALL :Set_Sequence !position!
    SET some_variable_!sequence!=%%A
    SET /A position+=1
)

FOR /F "tokens=1* delims==" %%A IN ('SET some_variable_') DO (
    ECHO "%%A" = "%%B"
)

EXIT /B

:Set_Sequence
SET sequence=!order:~%1,1!
GOTO :EOF

Actual output:

"some_variable_A" = "4"
"some_variable_B" = "5"
"some_variable_C" = "6"
"some_variable_D" = "1"
"some_variable_E" = "2"
"some_variable_F" = "3"

When your only tool is a hammer, every problem looks like a nail.


Report •

#5
March 23, 2012 at 19:34:57
✔ Best Answer
You don't even need to use call:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A position=0
SET order=DEFABC

FOR /L %%A IN (1,1,6) DO (
    for %%b in (!position!) do SET sequence=!order:~%%b,1!
    SET some_variable_!sequence!=%%A
    SET /A position+=1
)

FOR /F "tokens=1* delims==" %%A IN ('SET some_variable_') DO (
    ECHO "%%A" = "%%B"
)

EXIT /B



Report •

#6
March 24, 2012 at 08:37:17
Thanks guys :-)

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question