Timothy Leavitt · Jan 5, 2016 1m read

Nifty COS syntax - set $listbuild(multiple,variables) = list

(Possibly?) little-known fact: It's possible to use $ListBuild on the left hand side of the equals sign in a set command to extract multiple list values at once:

set $ListBuild(several,individual,variables) = list


For example:

USER>set colors = $ListBuild("red","orange","yellow")
USER>set $ListBuild(r,o,y) = colors
USER>write r,!,o,!,y,!


See for reference:

This feature was news to me - I use $ListBuild lists a lot but hadn't come across it until today. Had anyone else encountered this?

0 386
Discussion (4)4
Log in or sign up to continue

The syntax is great as it exactly expresses what you want to achieve, however if your code is performance critical it is faster to:

Set r=$list(colors,1),o=$list(colors,2),y=$list(colors,3)

Hopefully the 'set $lb' syntax will be optimized at some point as it is clearer than the multiple $list alternative and it should be possbible to make it faster.

I know of this syntax. However, the "problem" to me here is that all invididual variables declared here are implicitly created :/

If you create a lot of variables this way, this can become very hard to track what the code exactly does...

That's true.

Some developers like to use #dim to declare all the variables they expect to create. In Studio, Tools > Options..., Environment > Class, there's an "Option Explicit" option that will give you warnings if you use a variable that hasn't been #dim'd. (The "Track Variables" option is also very useful.)

These are the first two options I always enable in all my Studio environments. If you haven't made use of them yet, give them a try.