JSL Tip: Multiple Assignments

An often-overlooked feature of the JMP Scripting Language is the ability to assign values to multiple variables in a single assignment statement. A basic example is:

// assign 1 to x and 100 to y

{x, y} = {1, 100};

The syntax is especially useful when the values of the variables depend on previous values. For instance, an easy (and fast) way to swap the values of two variables is:

// swap x and y

{x, y} = Eval List( {y, x} );

Here you can see the advantage of the multiple assignment. Without it, you'd have to add a temporary variable and use three assignment statements. `Eval List`

is needed because when JMP evaluates a list, it doesn't evaluate the list members, by default.

Another use is when you are performing an iterative calculation on two or more mutually dependent variables. Euclid's greatest common divisor algorithm is one such calculation.

// Greatest Common Divisor of two integers

gcd = Function( {a, b},

while ( b != 0,

{a, b} = Eval List( {b, Mod(a, b)} );

);

a

);

gcd(45, 60); // 15

Insert a `Show(a, b)`

statement after the assignment to watch how the algorithm works.