Written by

Senior Development Manager at InterSystems Corporation
Article 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>kill
 
USER>set colors = $ListBuild("red","orange","yellow")
 
USER>set $ListBuild(r,o,y) = colors
 
USER>write r,!,o,!,y,!
red
orange
yellow

See for reference: http://docs.intersystems.com/cache20152/csp/docbook/DocBook.UI.Page.cls…

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?

Comments

Mark Hanson · Jan 6, 2016

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.

0
Francis Galiegue · Apr 27, 2016

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...

0
Timothy Leavitt  Apr 27, 2016 to Francis Galiegue

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.)

0
Stefan Wittmann  Apr 27, 2016 to Timothy Leavitt

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.

0