Passing array variables to Method: What type?

445 views
Skip to first unread message

Micky Hulse

unread,
Jul 15, 2010, 4:45:24 PM7/15/10
to Caché, Ensemble, DeepSee
Hi,

Let's say I have:

==========

new foo
foo("bar", "baz")
foo("bar", "hello world")
do someMethod(foo)

==========

What "type" would the array variable argument be? Would %String
suffice? Is there such a thing as %StringMultidimensional?

Sorry if silly question. :(

Thanks!
Micky

Micky Hulse (RG)

unread,
Jul 15, 2010, 4:49:03 PM7/15/10
to Caché, Ensemble, DeepSee
> foo("bar", "baz")
> foo("bar", "hello world")

Doh, typo in my "fake" code... It should read:

==========

set foo("bar", "baz") = "stuff"
set foo("bar", "hello") = "world"

==========

Sorry. :(

Jason Warner

unread,
Jul 15, 2010, 5:20:40 PM7/15/10
to intersystems...@googlegroups.com
I've always just used %String as my parameter when passing them. I
work mostly in mvBasic, but I seem to recall it working properly in COS.
One caveat in mvBasic: Make sure you dim foo() inside of your function
or you will get undimensioned array errors.

Jason

Micky Hulse

unread,
Jul 15, 2010, 5:31:26 PM7/15/10
to Caché, Ensemble, DeepSee
>   I've always just used %String as my parameter when passing them. I
> work mostly in mvBasic, but I seem to recall it working properly in COS.
> One caveat in mvBasic: Make sure you dim foo() inside of your function
> or you will get undimensioned array errors.

Thank you Jason! I really appreciate it. :)

Sungung

unread,
Jul 15, 2010, 7:07:26 PM7/15/10
to Caché, Ensemble, DeepSee
Hi Micky,

Array cannot be passed as value, it should be passed as reference(aka
pointer).
Cache syntax is you need to add a full stop like this do
someMethod(.foo)
Please see the below sample codes.

HTH, Sungung

ClassMethod Temp(ByRef arr As %String) As %Status
{
write !,arr("bar","baz")
set arr("bar","baz")="monkey"
quit $$$OK
}

ClassMethod Test()
{
set foo("bar","baz")="stuff"
set foo("bar","hello")="world"
do ..Temp(.foo)
Write !,foo("bar","baz")
}

Micky Hulse

unread,
Jul 15, 2010, 7:13:03 PM7/15/10
to intersystems...@googlegroups.com
Hi Sungung! Many thanks for the help. :)

> Array cannot be passed as value, it should be passed as reference(aka
> pointer).
> Cache syntax is you need to add a full stop like this do
> someMethod(.foo)
> Please see the below sample codes.

Ah, that is good to know. I am glad I asked the pros for help! :D

Thanks so much for the clarification, I really appreciate it.

Have an excellent day.

Cheers,
Micky

Gordon Clark (Herald Interactive)

unread,
Jul 16, 2010, 10:08:38 AM7/16/10
to Caché, Ensemble, DeepSee
Hey Sungung,

Do you know how one would do this in cache objectscript if they wanted
to pass the data from the %request object?

Like let's say I'm writing a function that takes two arrays and
returns a new merged one at the end?

<script language="cache" method="mergeArrays"
arguments="array1:%String, array2:%String" returntype="%String">

How would I pass %request.Data?

set foo = mergeArrays(%request.Data, newArr) // would return an
undefined error

I tried doing .%request.Data and .(%request.Data) to try to emulate
what you had done above, but that gives me a "missing right
parenthesis" error.

Sungung

unread,
Jul 18, 2010, 12:08:58 AM7/18/10
to Caché, Ensemble, DeepSee
Hi Gordon,

%rqeuest.Data is not variable name, it is property of object,
so .varname syntax is not working for this example.
And surprisingly found method in CSP page does not support passing by
reference, you may ask this Intersystems.
Alternative solution is using merge command. merge newArr =
%request.Data

HTH, Sungung

On Jul 17, 12:08 am, "Gordon Clark (Herald Interactive)"

Eric

unread,
Jul 18, 2010, 9:04:21 AM7/18/10
to Caché, Ensemble, DeepSee

%request is a public variable that is available everywhere so you
don't need to pass it.

Micky Hulse

unread,
Jul 18, 2010, 3:08:49 PM7/18/10
to intersystems...@googlegroups.com
Hi Eric and Sungung,

> %request is a public variable that is available everywhere so you
> don't need to pass it.

Good to know! I was kinda wondering about that.

Question: If you wanted to manipulate %request.Data, but not change
the current %request.Data, would %ConstructClone() be the appropriate
method to use?

Example code (tested, and appears to work):

<script language="cache" runat="server">
#define qs(%r, %key, %val) set %r.Data(%key, $order(%r.Data(%key, ""),
- 1) + 1) = %val // Macros rock!
set r = %request.%ConstructClone()
$$$qs(r, "foo", "2")
$$$qs(r, "subsection", "all")
#undef qs
</script>

The above is what I suggested to Gordon (%ConstructClone()), but I am
far from being a pro COS/CSP/Caché guru (like ya'll)... Just curious
if my suggestion is a valid approach, or if using %ConstructClone() on
%request is a bad idea (might be overkill if all you need to
manipulate is the query string key/value pairs)?

A billion thanks for the help!

Have a great day/night.

Cheers,
Micky

Eric

unread,
Jul 18, 2010, 3:51:46 PM7/18/10
to Caché, Ensemble, DeepSee

Micky,

If you want you own copy of %request.Data there is no need to clone
the entire object, just merge the property:

Merge MyCopy = %request.Data

Look up the MERGE command in the documentation.

Eric

Gordon Clark (Herald Interactive)

unread,
Jul 19, 2010, 11:35:09 AM7/19/10
to Caché, Ensemble, DeepSee
Hey Sungung,

Thanks for the response. I know that ObjectScript does let you pass by
reference with objects. But, I am surprised to find that instead of
providing a reference when I try to use an array, it just gives me a
null/undefined message. Perhaps the best method is to use one of the
array objects provided in the %Library package to pass this data
around.

Do you know if any InterSystems guys ever read these threads?

On Jul 18, 12:08 am, Sungung <sungung.p...@gmail.com> wrote:

kevin furze

unread,
Jul 19, 2010, 12:20:03 PM7/19/10
to Caché, Ensemble, DeepSee
> Do you know if any InterSystems guys ever read these threads?

yes, they do, but that doesn't mean they are going to react, you need
to put this through the official WRC channels, that way, they have a
record of the issues, and the request. If nothing else, they will
reject it, but on the other hand, they may just do something.

Micky Hulse

unread,
Jul 19, 2010, 12:39:10 PM7/19/10
to intersystems...@googlegroups.com
> If you want you own copy of %request.Data there is no need to clone
> the entire object, just merge the property:
> Merge MyCopy = %request.Data
> Look up the MERGE command in the documentation.

Boy, am I glad I asked. :)

Thanks! I really appreciate the clarification.

Have a great week!

Cheers,
Micky

Reply all
Reply to author
Forward
0 new messages