· Aug 31, 2019

What do You Use For Static Array Management in ObjectScript: List vs Global vs Local vs PPG

Hi Developers!

Often we need to use relatively small arrays with constants, static arrays in algorithms, etc where we need to do something with each element of an array. There are several ways to deal with it in ObjectSctipt. 

Previously I used to use globals, locals, PPG for this but not so long time ago figured out that %List is a way too handy.

Indeed, suppose we have an array of months and need to set up and use it in our code.

Globals way:


ClassMethod MonthGlobalSetup(ad)


for i="January","February","March","April","May","June","July","August","September","October","November","December" {

s @ad@($I(@ad))=i




ClassMethod MonthGlobalUsage()


set ad=$na(^Month) k @ad

d ..MonthGlobalSetup(ad)

// get the particular value by index

W "Month N5 is ",@ad@(5)

// work with all entries of the global level

set monthId=$Order(@ad@(""))

while monthId'="" {

w monthId," month is ",@ad@(monthId),!

set monthId=$Order(@ad@(monthId))



List way.  Setup:

ClassMethod GetMonthList() As %List


Return $Listfromstring("January,February,March,April,May,June,July,August,September,October,November,December")



ClassMethod MonthListUsage()


set monthList=..GetMonthList()

// get the particular value by index

w "Month N5 is ",$Listget(monthlist,5)

// work with all entries of the list

set iter=0,id=0

while $Listnext(monthList,iter,month) {

w $seq(id)," month is ",month,!



Lists usage looks more readable and doesn't set into any Persistent global in Namespace which is needed to be documented. 

And 'While' iteration through all the values is much clearer with lists.

My preference now for static arrays is List.

What's yours?

What do you use to handle static arrays in ObjectScript
Discussion (15)1
Log in or sign up to continue


ClassMethod GetMonthList1() As %List
    Quit $Listfromstring("January,February,March,April,May,June,July,August,September,October,November,December")

instead of

ClassMethod GetMonthList3() As %List
 Quit $lb("January","February","March","April","May","June","July","August","September","October","November","December")


Parameter is about 6x faster btw:

ClassMethod GetMonthList1() As %List
    Quit $Listfromstring("January,February,March,April,May,June,July,August,September,October,November,December")

ClassMethod GetMonthList2() As %List [ CodeMode = expression ]

ClassMethod GetMonthList3() As %List [ CodeMode = expression ]

Parameter GetMonthList = {$lb("January","February","March","April","May","June","July","August","September","October","November","December")};

ClassMethod Time2()
    for method = 1:1:3 {
        set start = $zh
        for i=1:1:10000 {
            set result = $classmethod(,"GetMonthList" _ method)
        set end = $zh
        write $$$FormatText("Method: %1, time: %2", "GetMonthList" _ method, end - start),!
    set start = $zh
    for i=1:1:10000 {
        set result = ..#GetMonthList
    set end = $zh
    write $$$FormatText("Parameter, time: %1",  end - start),!


Method: GetMonthList1, time: .007255
Method: GetMonthList2, time: .006717
Method: GetMonthList3, time: .003878
Parameter, time: .000605

It depends on a use case.

  • For access by position use $lb.
  • For access by key use locals.

Consider the scenario of iterating over the result set. You need to do two things:

  • Convert value based on a column datatype
  • If a value is special translate it

To solve the first one (and assuming we get values by position number) the most efficient solution would be iterating over metadata once and building something like this:

set columns = $lb("int", "bool", "str", ...)

Then in each row we can easily get datatype by:

for i=1:1:colCount {
  set dataType = $lg(columns, i)
  set value = rs.Get(i)

Now for a second part - value translation, locals are great here. We prep our translator:

set local(1) = "a"
set local("hi") = "world"

and replace the value if needed

set:$data(local(value), newValue) value = newValue

Thanks, Ed! Two remarks:

1. You mention iteration over resultset and introduce an example of iteration over array of columns cortege or record.

2. My question is not about getting values from arrays like list, locals, ppg, globals but what is better to use when you have a case of full scan over the array that in some cases lists are handier vs locals etc. 

Though the question of what array is better when you need to get values for keys is interesting too, maybe it's another good topic for discussion. Answering your statement here I agree for LB the choice for value by position if it's not a very large (huge) array, where the global is the only answer.

1. Before we iterate over rows we need to determine column type, the example is about that.

Here's an example of what I'm talking about.

2. You want to avoid full scan as much as possible. Two techniques for that are:

  • knowing position
  • knowing key

And that determines the type of structure used.

If you don't know either you need to think about possible algorithm improvements.

I agree that there are cases where we need to do something over each element of the collection.

But you said:

constants, static arrays in algorithms, e.g. arbitrary dictionaries

Which implies that foreach iteration is not desired here.

Anyways, in that case what does matter is size.

If you're sure that it would be less than $$$MaxStringLength then you can use $lb, otherwise use locals (which can be converted to ppg/globals easily).

For me the main acceptable case of foreach iteration is business objects - for example we receive array of results and must process them. In that case we, of course, must iterate over them, no way around it.

Everything static: constraints, enums, dictionaries, etc... should be (and usually could be) used without foreach iteration.