Following on to this, I discovered that multi-dimensional arrays seem to work just fine if you initialize your array as noted above. This is CRAZY impressive. Check this out:
/***********************************************************************************
*
* Function: uHxOBS
* Author: Greater Lawrence Family Health Center
*
* v2016.02.24: (CAD) First release version
* v2016.02.28: (CAD) Add trim array.
*
* Documentation: This function returns an array of historical observations for an
* array of OBSTERMs.
*
* Arguments:
* 1. Array (string) of OBSTERM names
* The first OBSTERM in this array establishes the anchor set of
* OBSTERM values and observation dates. Therefor, it is important
* that OBSTERM with the most frequent observations or most critical
* observation dates be the first OBSTERM in the array.
* 2. Maximum number (integer) of historical values to retrieve. If
* value is set to 0 then no limit is applied.
* 3. Array (string) [OPTIONAL] of T (trim) or F (no trim) to indicate
* whether each OBSTERM should be trimmed (i.e., drop any text after
* the first space encountered). This necessary because some OBSTERMS
* are "dirty" and have extraneous data included after the obsterm
* value. The default if this array is not specified is to always trim.
*
* See Testing Code below for sample usage.
*
************************************************************************************/
fn uHxOBS(aaO, iaD, aaT){
local iO local aO local iD local aOD local sL local iT local oV
iO = size(aaO) /* Num OBSTERMs */
aO = array() /* Array obs */
iD = 0 /* Num dates */
/* Create 3D array of OBSTERM observations where:
* aO[*] are OBSTERMs
* aO[1][*] are obs for OBSTERM[1]
* aO[1][2][*] are obs values for DATE[2] for OBSTERM[1]
*/
for i=1, i <= iO, i = i+1 do /* OBSTERMS */
aO[i] = getfield(LIST_OBS(aaO[i],"pencil","delimited","value"),"|","")
iD = size(aO[i])
for j=1, j <= iD, j = j+1 do /* Dates */
aO[i][j] = getfield(aO[i][j],"^","")
endfor
endfor
/*
* Create array of obs dates from the anchor OBSTERM, then walk each
* OBSTERMs obeservations for matching obs dates.
* aOD[*] are obs dates
* aOD[1][1] date of observation 1
* aOD[1][2] first OBSTERM on obs date[1][1]
*/
aOD = array()
sL = array()
iD = size(aO[1])
iT = 0
if iaD <> 0 AND iD > iaD then iD = iaD endif /* Max */
for i=1, i <= iD, i = i+1 do /* Dates */
aOD[i] = aO[1][i][2]
for j=1, j <= iO, j = j+1 do /* OBSTERMs */
for k=1, k <= size(aO[j]), k=k+1 do /* Scan observations */
sL = getfield(aOD[i])
if sL[1] = aO[j][k][2] then /* Date exists? */
oV = aO[j][k][1]
if ok(aaT) then
if aaT[j] == "T" then
if match(oV," ") then oV = remove(oV, match(oV," ")) endif
endif
else
if match(oV," ") then oV = remove(oV, match(oV," ")) endif
endif
aOD[i] = aOD[i] + "," + oV
iT = 1
endif
endfor
if iT then iT = 0 /* Found */
else aOD[i] = aOD[i] + "," /* Not found */
endif
endfor
endfor
return aOD}
/**
Testing Code:
local atestO
atestO = array("WEIGHT","BMI","HGB","HCT","PLATELETS")
uHxOBS(atestO,15)
**/
Posted : April 23, 2016 8:44 am