• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to evaluate an env-var, created at runtime?

Aug 12, 2014
13
0
#1
Probably quite simple, but I'm stuck anyway:

Let's say, I have an env-var 'idx' containing an index, e.g. 0,1,2
Than there are env-vars str_0, str_1, str_2, containing strings
Finally I want to use one of these strings, together with a command call, like
my_exe.exe <string>
with <string> shoud be the string contained inside str_<%idx%>

How can I call my_exe.exe to do this?

I have something like
my_exe.exe %@concat["str_"[email protected]_env[idx]%
in my mind, but have not found the @-functions doing this.

Currentliy I just need it for TC, but if there is a solution which can be used for CMD too, this would even better!

Thanks a lot

Michael
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,498
45
Albuquerque, NM
prospero.unm.edu
#2
In TCC, you should be able to use e.g.
Code:
my_exe.exe %[str_%idx]
CMD.EXE doesn't support anything like this syntax. If you want to be CMD-compatible, then it gets much, much uglier. I think you'd have to dynamically create and call a secondary batch file to evaluate the nested variable reference.
 
Aug 12, 2014
13
0
#3
Works fine. Thanks a lot...

Would it be better to write
my_exe.exe %[str_%idx]
or
my_exe.exe %[str_%idx%]%

As it seems in this example-case both work with the same result, but in other cases there might be some confusion with follow-up chars.

As a matter of fact, I just stumbled over another case; I'm not really sure, if it is not a bug (in my Version v18.00.32 x64):
set tst1=1
set tst_%tst1%=x
After the second line I got the error-message, translated to
TCC: R:\tst.bat [2] Not in the environment "tst_1x*"
Strange Thing is, when writing
set tst_%tst1=x
instead, all works fine, i.e. no error-msg and
set tst_1
returns
x
.

I have learned to surround all env-vars with % (before and after the var), except the args of the batch-file %0, %1, ...
So I'm a little confused: is my rule really right?

Thanks a lot,
Michael
 
Last edited:
Aug 12, 2014
13
0
#4
I checked my above example with CMD and there it works fine as intended.
So I suppose, this is really a bug in TC (v18.00.32 x64).

But I found out, that writing
set tst1=1
set tst_%[tst1]=x
instead works in TC as intended, but changing the second line to
set tst_%[tst1]%=x
results in the same error as before.

So I suppose using %[...] is a correct and complete expression (and not %[,,,]%).
Same rule with e.g. %@eval[...] .

Thanks,
Michael
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,498
45
Albuquerque, NM
prospero.unm.edu
#5
So I suppose using %[...] is a correct and complete expression (and not %[,,,]%).
Same rule with e.g. %@eval[...] .
That's correct. The right bracket signals the end of the function (or the indirect variable reference, in your first case). Once the parser finds that right bracket, it knows it has reached the end.

More complex for variables; the trailing percent sign is usually optional. But you will need it if the next character would be legal in a variable name: a letter, digit, underscore, or dollar sign.
 
Top Bottom