Question
Emanuel Lazar · Apr 30, 2021

cache/iris class array approach analogue to javascript Associative array

objects in javascript :

var oM = new Object;

var mMode=["down","up","click"] ;
var key,val ;
    for (key in mMode)
    {

       val = mMode[key] ;
        oM[val]={} ;
        oM[val].id   ="" ;
        oM[val].type ="" ;

       ...

 }

so I can access the .type directly as :

oM.down.type

or:

oM["down"].type

what is the syntax in CACHE or IRIS, to achieve such approach via class/key/property...?

acctually: class/object/key/property anlaogue to ^GLOBAL(key,"property")=val

 

 

00
2 0 9 92
Log in or sign up to continue

Replies

I think you're looking for the %ArrayOfObjects class for this one. You'd create your objects with all of their value, ID, and type properties, then you'd create the array:

set array = ##class(%ArrayOfObjects).%New()

Then you set values of the array using the SetAt method:

do array.SetAt(downobject,"down")

Then to access a particular value, you use the GetAt method, then dot syntax to access the object's properties:

set myid = array.GetAt("down").id

Here's the %ArrayOfObjects class documentation.

>do array.SetAt(downobject,"down")

how I set the: .id=val and .type=val1

ClassMethod TestObj() As %DynamicObject

{
        Set oM = {}
        Set mMode = ["down","up","click"]
        Set iter = mMode.%GetIterator()
        While iter.%GetNext(,.val)
        {
            Do oM.%Set(val,{"id":"","type":""})
        }
        Quit oM
}

USER> set oM = ##class(User.DynObj).TestObj()
USER> write oM.%ToJSON()
{"down":{"id":"","type":""},"up":{"id":"","type":""},"click":{"id":"","type":""}}
USER> zwrite oM.down.id
""
USER> zwrite oM.up.type
""
USER> set oM.click.type = "double"
USER> write oM.click.type
double
USER> write oM.%ToJSON()
{"down":{"id":"","type":""},"up":{"id":"","type":""},"click":{"id":"","type":"double"}}

Hmm. Upon re-reading your question, I realized this isn't really the answer you were looking for. Others have provided the correct solutions/references.

>

>set oM.click.type = "double"

can I set also :

oM["click"].type="double" ?

is the oM, persistent, like set ^oM("click","type")="double" ?

or actually, I want to set "^global" values by objects/methods syntax, but maintain the direct approach of ^globals ?

not using some clumsy .openId(?)

just get/set values 

No, you can't use the quoted string notation (or at least if you can, I haven't figured out how).

It's not persistent either, which is why I followed up to state that it's not really what you're looking for.

In additional experimentation, I found that there is a quoted-string mechanism:

> Set oM."click".type = "double"

is equivalent to

> Set oM.click.type = "double"

Regardless, it's still not persistent.