Common pitfalls

Lists syntax errors you should avoid when writing IBM® ILOG® Script statements in OPL models.

No range syntax

The range syntax you can use in OPL modeling statements does not exist for script statements.

In OPL, a <= x <= b is equivalent to a <= x && x <= b,

whereas in scripting it is evaluated as ((a <= x) <= b) or

var v1 = a <= x;

v1 <= b;

However, that syntax is valid for JavaScript (ECMAScript) parsing in some cases.

For example, this statement:

 for(var i in 1..n) 

iterates an empty loop. The expression “1..n ” is interpreted as the named property n for the number object 1. As that property does not exist, it evaluates to undefined. Iterating the undefined value is an empty loop.

No tuple syntax

The tuple syntax in OPL modeling statements does not exist for script statements. Use the find() or the get() methods to get control of tuple objects. For example, instead of writing:

 A[<a,b>] 

which results in a parsing error, write

 A[S.get(a,b)] 

where S is the indexer for A.

IBM ILOG Script variables

All variables you declare using the var keyword in a scripting block are undefined in the block until they are declared. For example:

int a=2;
execute {
  writeln(a);
var a=2;
  writeln(a);
}

gives out

 undefined 2

Lazy initialization of data

See Pitfall of lazy initialization of data.