Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
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.

Article Labels

There are no labels assigned to this post.

Article Tags
1 Comment
Visitor

Mark Bailey wrote:

I often use this idiom, since I learned that lists could serve as a L-value. Lists make for efficient and compact yet readable code. Here are two of the common uses for me:

// confidence interval of the mean

sampleN = 10;

sample = J( sampleN, 1, Random Normal( 10, 1 ) );

yBar = Mean( sample );

sdY = Std Dev( sample );

{lo95%CI, hi95%CI} = Round(

yBar + {-1, 1} t Quantile( 0.975, sampleN-1 ) sdY / Sqrt( sampleN ),

2

);

Show( lo95%CI, hi95%CI );

// y axis scale

{yMin, yMax} = Round(

yBar + {-4.5, 4.5} * (Maximum( sample ) - Minimum( sample )),

1

);

Show( yMin, yMax );