Hack Forums
[Shell] Is it possible for a variable inside of a variable? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Shell] Is it possible for a variable inside of a variable? (/showthread.php?tid=4577232)



Is it possible for a variable inside of a variable? - Topkek_Is_Life - Dec 9, 2014

I'm pretty rusty with batch and am looking for a way to do this or a way around it.
The script is
Code
set var%setnum%= %str:~!str1!,!str2!%
Where %setnum% is equal to 1, %str% is equal to the alphabet (ABCD...), %str1% is equal to 0, and %str2% is equal to 1.
so that it would display like
Code
Set var1= ABCDEFGHIJKLMNOPQRSTUVWXYZ:~0,1
That way if I were to echo %var1% is would display "A"


* - WOLGA - Dec 9, 2014

You want the variable's name to correspond with a letter of the alphabet?
Code
setlocal enabledelayedexpansion
set str=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for /l %%A in (0,1,25) do (
set /a num=%%A+1
set var!num!=!str:~%%A,1!
)



* - White Light - Dec 9, 2014

(Dec 9, 2014 03:01 PM)Topkek_Is_Life Wrote:  I'm pretty rusty with batch and am looking for a way to do this or a way around it.
The script is
Code
set var%setnum%= %str:~!str1!,!str2!%
Where %setnum% is equal to 1, %str% is equal to the alphabet (ABCD...), %str1% is equal to 0, and %str2% is equal to 1.
so that it would display like
Code
Set var1= ABCDEFGHIJKLMNOPQRSTUVWXYZ:~0,1
That way if I were to echo %var1% is would display "A"

When you're storing a variable, you're storing the actual value of the variable and not how to get the variable. It will expand that entire line, and then perform what it says.