Question
· Aug 27, 2021

How to iterate a local array with $QUERY() in a job ?

In Caché, it's possible to iterate a local array using $QUERY() :

set a("foo") = 50
set a("bar") = 30

set key = "a"
for
{
     set key = $QUERY(@key)
     quit:key="" 
     write key_" = "_@key
}

This code works perfectly, unless it's called in a job (eg: a routine called by the JOB command).

In that case, $QUERY will return an empty string. It's like the array cannot be referenced anymore by simply using it's name.
It works using $ORDER(a("")) but I would like to use $QUERY if possible (because my array has several dimensions and it would require several for loops).

Discussion (3)0
Log in or sign up to continue

OK!  you talk about a local variable array
If you don't pass the local array with the JOB command it is not defined in the new JOB
because it is LOCAL   
except if you run  the 2 SET commands of your example also in the JOBbed routine.
also your WRITE command will fail if you don't specify some output device.
since by definition a background JOB doesn't have foreground output.

You are on a right way, but obviously you use indirection on local variables in block environment.

the wrong way:

Method testErr()
{
   set myvar(1)=123, ref=$na(myvar)
   set ref=$query(@ref)  -->  this gives you always: ref=""
}

The correct way

Method testOK() [ PublicList = myvar]
{
   new myvar
   set myvar(1)=123, ref=$name(myvar)
   set ref=$query(@ref) 
   write ref," ",@ref ---> myvar(1)," ",123
}

i.e. indirection needs variables with global scope