Optoscript Eval() function?


#1

Does Optoscript have something equivalent to an Eval() function? I haven’t been able to find anything.

For those unfamiliar, Eval lets you pass expressions or operators in a string, and act on them.

As an example, the old project I’ve taken over makes use of very generically named tables, Digi0, Digi1, Digi2, etc. Dozens upon dozens of them, and they have the same set of operations performed on them.

Currently it goes something like this:

Digi0[23] = 4;
Digi1[23] = 4;
...
Digi45[23] = 4;

Whereas with Eval, it would be much simpler:

for i = 0 to 45 step 1
eval ("Digi" + i + "[23] = 4;");
next

PAC Control 101: Why/when/how would I use POINTERS?
#2

Welcome to the OptoForums, dwilson!

Good question. While there’s no exact equivalent, you can use a pointer to a table and the command GetPointerFromName to do the same thing, perhaps something like this:

for nDigiXIndex = 0 to 45 step 1

  NumberToString( nDigiXIndex, sDigiXTagName );

  sDigiXTagName = "Digi" + sDigiXTagName;

  GetPointerFromName( sDigiXTagName, ptrDigiX );

  if ( ptrDigiX == null ) then // we didn't find a Tag with that name, log error
    
    sErrorMessage = "Could not find tag named: " + sDigiXTagName; 

    AddMessageToQueue( 16, sErrorMessage );

  else
    
    *ptrDigiX[23] = 4;

  endif

next

Of course, if someone renames those tables this logic will break (but so would the eval(), I believe), which is why I check for null in there. I hope that helps/makes sense?

-OptoMary


#3

It makes sense, and it sure does help. I knew there had to be a way to do it, just couldn’t find it on my own.

Thanks! You just helped me turn 5k+ lines of spaghetti-code in to about 200.