Windows batch variables

I have a problem getting the value from a variable called by two other variables. It may seem complicated because my English is not the best.

I use the rXY variable to make an xy grid, where X is x pos and Y is y pos. I filled the grid with random characters, but now I want to get the value of a single point.

echo% r23% will work, but if I ask the user for coordinates (set / P p_x =), then echo% r% p_x %% p_y %% will not work.

Is it even possible to get such a value?

Code example:

echo off
cls
FOR /L %%Y IN (1,1,4) DO (FOR /L %%X IN (1,1,4) DO set r%%X%%Y=.)

echo Y
echo 4 %r14% %r24% %r34% %r44%
echo 3 %r13% %r23% %r33% %r43%
echo 2 %r12% %r22% %r32% %r42%
echo 1 %r11% %r21% %r31% %r41%
echo 0 1 2 3 4 X

set /P input=Please enter X and Y pos:
set p_x=%input:~0,1%
set p_y=%input:~1,2%
echo X=%p_x%
echo Y=%p_y%
echo [2,3]=%r23%

echo %r%p_x%%p_y%
pause
+3
source share
2 answers

Use (terrible) slow expansion .

setlocal enabledelayedexpansion
echo !r%p_x%%p_y%!

%variables%expands when parsing a string !variables!- during actual execution.

+3

-:

CALL ECHO %%r%p_x%%p_y%%%

, ( , ).

. % %, , , , %, .

, . () CALL ECHO %r21%.

, CALL, , %r21% .

+2
source

Source: https://habr.com/ru/post/1795843/


All Articles