Article
· 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?

Discussion (4)1
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.