Skip to main content

SET $LISTBUILD

SET $LISTBUILD

When used on the left side of the equal sign in a SET command, the $LISTBUILD function extracts multiple elements from a list as a single operation. The syntax is as follows:

SET $LISTBUILD(var1,var2,...)=list

The var arguments of SET $LISTBUILD are a comma-separated list of variables, each of which is set to the value of the list element in the corresponding position. Thus, var1 is set to the value of the first list element, var2 is set to the value of the second list element, and so forth. The var arguments do not have to be existing variables; the variable is defined when SET $LISTBUILD assigns it a value.

  • The number of var arguments may be less than or greater than the number of list elements. Unspecified var values retain their prior value; if previously undefined they remain undefined. Compare this behavior with SET $LISTGET. Excess list elements are ignored.

  • The var arguments and/or the list elements may contain omitted values, represented by placeholder commas. An omitted var argument is undefined. An omitted list element causes the corresponding var value to retain its prior value; if previously undefined it remains undefined. Compare this behavior with SET $LISTGET.

SET $LISTBUILD is an atomic operation. The maximum number of var arguments in a compiled program is 1024. The maximum number of var arguments when executed from the Terminal is 128. Attempting to exceed these limits results in a <SYNTAX> error.

If a var argument is an object property (object.property) the property must be multidimensional. Any property may be referenced as an i%property instance variable within an object method.

In the following examples, $LISTBUILD (on the right side of the equal sign) creates a list with four elements.

In the following example, SET $LISTBUILD extracts the first two elements from a list into two variables:

   SET colorlist=$LISTBUILD("red","blue","green","white")
   SET $LISTBUILD(a,b)=colorlist
   WRITE "a=",a," b=",b   /* a="red" b="blue" */

In the following example, SET $LISTBUILD extracts elements from a list into five variables. Because the specified list does not have a 5th element, the corresponding var variable (e) contains its prior value:

   SET (a,b,c,d,e)=0
   SET colorlist=$LISTBUILD("red","blue","green","white")
   SET $LISTBUILD(a,b,c,d,e)=colorlist
   WRITE "a=",a," b=",b," c=",c," d=",d," e=",e
   /* a="red" b="blue" c="green" d="white" e=0 */

In the following example, SET $LISTBUILD extracts elements from a list into four variables. Because the specified list does not have a 3rd element, the corresponding var variable (c) contains its prior value:

   SET (a,b,c,d)=0
   SET colorlist=$LISTBUILD("red","blue",,"white")
   SET $LISTBUILD(a,b,c,d)=colorlist
   WRITE "a=",a," b=",b," c=",c," d=",d
   /* a="red" b="blue" c=0 d="white" */

In the following example, SET $LISTBUILD extracts elements from a list into four variables. Because the 3rd list element value is a nested list, the corresponding var variable (c) contains a list value:

   SET (a,b,c,d)=0
   SET colorlist=$LISTBUILD("red","blue",$LISTBUILD("green","yellow"),"white")
   SET $LISTBUILD(a,b,c,d)=colorlist
   WRITE "a=",a," b=",b," c=",c," d=",d
   /* a="red" b="blue" c=$LB("green","yellow") d="white" */
FeedbackOpens in a new tab