You are not logged in.
Pages: 1
Hi,
Is it possible to do an eval-like-thing in a BAT file?
The following doesn't totally work but it will show you what I am trying to do:
set array[0]=apple
set array[1]=banana
set i=0
:loop
if defined array[%i%] (
rem broken >> echo %array[%i%] << should print the value of array[i]
rem broken >> echo %array[%%i%%]% << should print the value of array[i]
set i=%i%+1
goto :loop
) else (
set i=
)
Thanks,
Si ++
Offline
try like this
set _var=array[%i%]
CALL SET _value=%%%_var%%%
echo %_value%
Offline
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set array[0]=apple
set array[1]=banana
set i=0
:loop
if defined array[%i%] (
echo !array[%i%]!
set /A i=%i%+1
goto loop
) else (
set i=
)
Offline
^ Thats a very good idea using ENABLEDELAYEDEXPANSION outside a FOR loop so the % delimiters dont get confused.
To get the value stored in the array (rather than the array item) I think you'll still need the CALL SET trick
Offline
^ Thats a very good idea using ENABLEDELAYEDEXPANSION outside a FOR loop so the % delimiters dont get confused.
To get the value stored in the array (rather than the array item) I think you'll still need the CALL SET trick
Nah, "echo !array[%i%]!" will get the value of the array item, I use that trick all the time.
cmd | *sh | ruby | chef
Offline
yeah you won't need any call set trick since I've never used it. I've been doing variable expansion for awhile now. But if you like it then don't stop using it, that's the one thing about batch files, once you've written dozens of 20K batch files because you're caught without a compiler then you realize that everyone does things differently and you're like no one else.
There is one situation where I think I had embedded if statements groupings (Both sets of brackets had a lot of code!) where I had to remove one of the sets and put it in a tagged section at the bottom of the file and call :tagNewSection it. That was the only time delayed variable expansion didn't work, but removing the embedded if section and having it called did the trick.
Offline
The "call" trick is actually relatively new to me, and there are some circumstances where I've had to use it to achieve my aim. Specifically in a script where I wanted to avoid "enabledelayedexpansion" to protect my precious "!" symbols, and not use a temporary file (which was impeding performance) to pull some data out of an array.
if %pramcount% GTR 0 (
for %%c in (%pramout%) do (
for /l %%d in (0,1,%lines%) do (
REM echo:set %tmpl%%%d=%%%tmpl%%%d%%%%%tag%%%cx%%d%%%%%tag%¿x%%d%%>>"%tempfl%"
call set %tmpl%%%d=%%%tmpl%%%d%%%%%tag%%%cx%%d%%%%%tag%¿x%%d%%
)
)
REM call "%tempfl%"
REM del "%tempfl%"
)
The REM'd out lines being superseded by simply using the call.
Yes, the strings of % were a complete pain to figure out. As I recall I just kept putting more in until it worked.
The script this came from, if you're still awake out there, behaves somewhat similarly to the UNIX "banner" command.
cmd | *sh | ruby | chef
Offline
Pages: 1