Question Norman W. Freeman · Feb 25

How to write at the end of a global ? (last node)

I have a global with multiple nodes : 

^A("ABC") = ""
^A("DEF") = ""
^A(123) = ""

How to create a new single node in that global, in a way it's always at the end of the global (eg: this will be the very last node being enumerated)

I end up with this code but maybe there is a cleaner and simpler approach : 

set ^A($char(65535)) = "" //should be bigger than any character inserted
set ^A($char(255)) = ""   //some systems does not seems to support >255 characters, this might be safer

I only need to write it once, this code is not intended to be used in a loop or something, just to add something at the end (like a footer).
Usually, the naked global would contains the key of the last node, but that's not something I can do in this case (my procedure is called by legacy code that I have no control over) 

set ^A = 1000
set ^A($I(^A)) = "" //will do set ^A(1001) = ""
Product version: IRIS 2024.1
$ZV: IRIS for Windows (x86-64) 2024.1.2 (Build 398U) Thu Oct 3 2024 14:01:59 EDT

Comments

Enrico Parisi · Feb 25

One option could be:

Set last=$o(^A(""),-1)

Set ^A(last_"z")="new last value"

In fact you can concatenate anything, not necessarily a "z"..

If the first subscript is always numeric, then:

Set last=$o(^A(""),-1)

Set ^A(last+1)="new last value"

0
Robert Cemper · Feb 25

The idea of 

set ^A($char(65535)) = ""

Looks good at first sight, but this might be the next follower

set ^A($char(65535),0) = ""
0
Timo Lindenschmid · Feb 26

I would be interested in the use case of having a special global subscript always at the end.
But essentially it comes down to convention and design. If you define your last global subscript to be e.g. "zzzz" then nobody should create entries with "zzzz1".
As i said for us to better advise we would need more information on what you need to achieve.

0
Norman W. Freeman  Feb 27 to Timo Lindenschmid

I have to maintain a legacy module that receive as input a local global variable (eg: it's like a list).

ClassMethod Foo(ByRef ITEMS) { }

It's too risky to refactor it (eg: use something else) as it's used in LOT of places. 

I need in some case to add a extra item at the end of that "list".

0
Timo Lindenschmid  Mar 1 to Norman W. Freeman

Hi Norman,

could you modify foo and do something like this at its beginning?

If $data(^SpecialcaseFlag) {
  // get last entry
  set idx=$order(ITEMS(""),-1)
  // set new last entry
  set idx=idx_"z"
  set ITEMS(idx)=......
}

0
Vitaliy Serdtsev · Mar 3

I do not know if multidimensional arrays are acceptable in your case or only one-dimensional arrays, so here is a general example.

Class dc.test Abstract ]
{

ClassMethod Foo(glbName)
{
  set ref = $query(@glbName@(""),-1)
  set @ref@("end") = "end"
}

ClassMethod Test()
{
  ^||A
  ^||A($c(65535)) = ""
  ^||A("DEF") = ""
  ^||A(123) = ""
  ^||A("ABC") = ""
  ..Foo("^||A")
  zw ^||A
}

}

USER>##class(dc.test).Test() ^||A(123)="" ^||A("ABC")="" ^||A("DEF")="" ^||A($c(65535))="" ^||A($c(65535),"end")="end"
0