You are not logged in.

#1 09 Feb 2007 13:13

Splat
Member
Registered: 09 Feb 2007
Posts: 6

Variable Evaluations

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

#2 09 Feb 2007 14:10

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Variable Evaluations

try like this

set _var=array[%i%]
CALL SET _value=%%%_var%%%
echo %_value%

Offline

#3 16 Feb 2007 19:10

Dan9999
Member
Registered: 16 Feb 2007
Posts: 7

Re: Variable Evaluations

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

#4 16 Feb 2007 21:16

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: Variable Evaluations

^ 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

#5 17 Feb 2007 11:23

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Variable Evaluations

Simon Sheppard wrote:

^ 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

#6 02 Mar 2007 19:22

Dan9999
Member
Registered: 16 Feb 2007
Posts: 7

Re: Variable Evaluations

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

#7 06 Mar 2007 00:37

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Variable Evaluations

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

Board footer

Powered by