Question Robert Shoults · Jul 27, 2021

How do I pass an array by reference and get back all modifications to the array (including subscript additions)

I have a routine tag that takes an argument and I want this argument to be an array reference.  So I try something like:

do mytag(.myarr)

The mytag tag, adds subscripts to myarr.

When I evaluate myarr after the tag call, only the subscripts passed in are retained in myarr.  The subscripts added by mytag are missing.  Is there a way to pass an array so it will behave the way I want it to?

Product version: Caché 2017.1
$ZV: Cache for Windows (x86-64) 2017.1.3 (Build 317_5_19833) Wed Apr 1 2020 11:36:20 EDT

Comments

David Hockenbroch · Jul 27, 2021

What's the definition of mytag? Is it using ByRef? If not, you'll want to add it.

0
Eduard Lebedyuk · Jul 27, 2021

Unable to reproduce. Here's my test routine:

ByRef
    kill a
    set a = 1
    set a(1) = 2
    do Test(.a)
    zw a

Test(a)
    set a(1) = a(1) + 1
    set a(2) = -1

And here's an invocation result:

>d ^ByRef
a=1
a(1)=3
a(2)=-1

as you can see a new subscript has been added successfully.

0
Pete Greskoff · Jul 27, 2021

Are you doing something like 'new myarr' in mytag? That would behave as you described.

0
Robert Shoults · Jul 27, 2021

I was passing the wrong number of arguments.  Totally a syntax error on my part.  Thanks for the replies.

0
Nigel Salm · Jul 28, 2021

I would have responded with a similar option:

classmethod ABC(byref p1 as %String(MAXLEN=*)) as %Status

set tSC=$$$OK

try {

     if $g(p1("{some_attribute_name}")) set .....

    set p1("{a_new_node"})="{a_value}")

    }

...

But if you wanted to keep it in proper objects pass in p1, byref, as a %ListOfDataTypes or %ListOfObjects or %ArrayOfDataTypes or %ArrayOfObjects depending on whether you are passing in an ordered list or and subscripted array

0