Variable indirection

Status
Not open for further replies.
May 24, 2010
855
0
Northlake, Il
I would like to place the name of a variable into another variable and then later evaluate the "outer" variable to get the value the "inner" variable. Similar to the following code sequence:

Set Sample=This is a sample
Set Indirect=Sample
Echo somethinginvolving Indirect

To yield:

This is a sample

It seems to me I've done this in the past, but I don't remember how I did it and have been unable to find out how to do it from the help system. Can somebody please either tell me how to do this or inform me of its impossibility?
 
May 30, 2008
235
2
Here's an example using the batch file parameters %1 %2 ... %N via indirection I recently used in my own batch code.

do i = 1 TO %#
set header=%header%``%[%i]``%@char[9]%``
enddo

That is, just use %[%variableToBeIndirected]

HTH
 
May 29, 2008
47
0
Orion Arm, MWG
Using the same values you used in your original post:

set sample=This is a sample
set indirect=sample


Just to confirm:

C:\> echo %sample
This is a sample

C:\> echo %indirect
sample


And I think this is what you're hoping to achieve:

C:\> echo %[%indirect]
This is a sample


For what it's worth, you're not restricted to using only variables
inside the [brackets]. You're also free to use functions (any valid
expression, really) as well. Have fun with it.


-- Dan McMullin
 
May 20, 2008
11,411
99
Syracuse, NY, USA
On Fri, 01 Oct 2010 01:24:25 -0400, mathewsdw <> wrote:

|I would like to place the name of a variable into another variable and then later evaluate the "outer" variable to get the value the "inner" variable. Similar to the following code sequence:
|
|Set Sample=This is a sample
|Set Indirect=Sample
|Echo somethinginvolving Indirect
|
|To yield:
|
|This is a sample

v:\> Set Sample=This is a sample

v:\> Set Indirect=Sample

v:\> echo %[%indirect]
This is a sample
 
May 24, 2010
855
0
Northlake, Il
Thanks, guys!!! But the only reason I'm not deleting this thread is to thank you for your answers! But I can't resist mentioning that the reason I was going go close and delete this thread is that I knew the answer; the reason it wasn't working was a really, really, really stupid error that I just didn't see! But, again, the problem is solved and thank you very much!!!

P. S. Obviously you guys are real nightowls like I am. Is this a programmer thing??? :)
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
T Variable indirection for arrays Support 1
D Parsing an array variable to an array does not work Support 5
sully9 How to? Unexpected %DATE% CMD variable format (vs DATE /T format) in CMDebug Support 1
P Inserting string into a variable. Support 3
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
R Difference between TCC and CMD: variable names with hyphens Support 4
Joe Caverly Get a variable type Support 2
M How to set Path variable for non-executable file extensions in 4DOS Support 8
rps For loop internal %_for_files variable? Support 2
A How to safely print variable's value without quotes? Support 9
vefatica Array name in a variable? Support 3
nikbackm TCC 20 variable expansion bug Support 6
X How to set %_INIREAD result to variable Support 5
Charles Dye _USBS variable Support 2
BeechComer Debugger: how to set %1 variable? Support 3
Mordachai Variable followed by function constantly failing... Support 5
Mike Hall Unexpected "Variable loop" in IF condition Support 10
vefatica If there's no PATH variable ... Support 5
C Maximum length for a variable Support 8
D WAD Internal variable _cpu is undefined Support 2
fpefpe How to? Global variable set /v Support 4
I Change value of a variable while debugging Support 3
I "IF" command in a variable does not expand redirection variable name Support 0
J Variable setting regression in tcc17 ? Support 5
Jay Sage WAD Variable _LOGFILE Not Working Support 2
D Fixed Change in behavior of variable expansion between 16 and 17 Support 3
rps Fixed Using last argument variable ! Support 5
Jay Sage Brackets Around Variable Names Support 11
D Internal variable _tctabs within TCExit Support 9
vefatica WAD Nested variable expansion in PDIR's @function[*]? Support 13
J Set environment variable from stdout of command Support 3
nickles Passing a variable to a command w/o adding it to the local environment Support 7
nickles WAD Variable name gets changed within FOR loop Support 2
nikbackm FUNCTION with variable number of arguments Support 17
rfaquino WAD Apparently a bug when expanding environment variable %ProgramFiles(x86) Support 2
S WAD %_do_errors does not report errors when loop control variable type is not directory entry Support 1
B WAD environment variable in TCC.exe Support 2
Frank watch an array variable in ide/bdebugger Support 2
vefatica Conditional expressions in a variable? Support 2
CWBillow UNSET a Variable Support 7
M Updating environment variable from C++ fails erratically in version 14... Support 57
Frank problem with environment variable x86 vs. x64 Support 2
Charles Dye Unbalanced parentheses in variable expansion Support 12
D WAD Multiple TEE causese weird variable scoping? Support 7
H Variable not expanding with detach Support 3
fpefpe volatile variable Support 5
T Bug in variable expansion Support 4
S New "home" variable parsing issue Support 1
H variable programfiles(x86) loses space Support 1

Similar threads