Variable indirection

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Status
Not open for further replies.
#1
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
209
1
#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
 
#3
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
 
#4
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
 
#5
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.