So as it turns out, I had my x and y flipped. I needed the arrays to be columns instead of rows so I could more easily add and average the totals. I did that and got this:
{
!fn calcAverages() {
local fastArray = ""
local PCBArray = ""
local ACLArray = ""
local PCLArray = ""
local ACDArray = ""
local PCDArray = ""
local QHSArray = ""
local matrixArray = ""
local OBSArray = ""
local avgOBSArray = ""
local dateSubCount = 14
local avgSum = 0
local fieldCount = 0
local finalAvg = 0
/*Arrays*/
/*These arrays work in columns instead of in rows. This makes it easier to add and average the totals of each column.*/
fastArray = array(DOCUMENT.FAST_1, DOCUMENT.FAST_2, DOCUMENT.FAST_3, DOCUMENT.FAST_4, DOCUMENT.FAST_5, DOCUMENT.FAST_6, DOCUMENT.FAST_7, DOCUMENT.FAST_8, DOCUMENT.FAST_9, DOCUMENT.FAST_10, DOCUMENT.FAST_11, DOCUMENT.FAST_12, DOCUMENT.FAST_13, DOCUMENT.FAST_14)
PCBArray = array(DOCUMENT.PCB_1, DOCUMENT.PCB_2, DOCUMENT.PCB_3, DOCUMENT.PCB_4, DOCUMENT.PCB_5, DOCUMENT.PCB_6, DOCUMENT.PCB_7, DOCUMENT.PCB_8, DOCUMENT.PCB_9, DOCUMENT.PCB_10, DOCUMENT.PCB_11, DOCUMENT.PCB_12, DOCUMENT.PCB_13, DOCUMENT.PCB_14)
ACLArray = array(DOCUMENT.ACL_1, DOCUMENT.ACL_2, DOCUMENT.ACL_3, DOCUMENT.ACL_4, DOCUMENT.ACL_5, DOCUMENT.ACL_6, DOCUMENT.ACL_7, DOCUMENT.ACL_8, DOCUMENT.ACL_9, DOCUMENT.ACL_10, DOCUMENT.ACL_11, DOCUMENT.ACL_12, DOCUMENT.ACL_13, DOCUMENT.ACL_14)
PCLArray = array(DOCUMENT.PCL_1, DOCUMENT.PCL_2, DOCUMENT.PCL_3, DOCUMENT.PCL_4, DOCUMENT.PCL_5, DOCUMENT.PCL_6, DOCUMENT.PCL_7, DOCUMENT.PCL_8, DOCUMENT.PCL_9, DOCUMENT.PCL_10, DOCUMENT.PCL_11, DOCUMENT.PCL_12, DOCUMENT.PCL_13, DOCUMENT.PCL_14)
ACDArray = array(DOCUMENT.ACD_1, DOCUMENT.ACD_2, DOCUMENT.ACD_3, DOCUMENT.ACD_4, DOCUMENT.ACD_5, DOCUMENT.ACD_6, DOCUMENT.ACD_7, DOCUMENT.ACD_8, DOCUMENT.ACD_9, DOCUMENT.ACD_10, DOCUMENT.ACD_11, DOCUMENT.ACD_12, DOCUMENT.ACD_13, DOCUMENT.ACD_14)
PCDArray = array(DOCUMENT.PCD_1, DOCUMENT.PCD_2, DOCUMENT.PCD_3, DOCUMENT.PCD_4, DOCUMENT.PCD_5, DOCUMENT.PCD_6, DOCUMENT.PCD_7, DOCUMENT.PCD_8, DOCUMENT.PCD_9, DOCUMENT.PCD_10, DOCUMENT.PCD_11, DOCUMENT.PCD_12, DOCUMENT.PCD_13, DOCUMENT.PCD_14)
QHSArray = array(DOCUMENT.QHS_1, DOCUMENT.QHS_2, DOCUMENT.QHS_3, DOCUMENT.QHS_4, DOCUMENT.QHS_5, DOCUMENT.QHS_6, DOCUMENT.QHS_7, DOCUMENT.QHS_8, DOCUMENT.QHS_9, DOCUMENT.QHS_10, DOCUMENT.QHS_11, DOCUMENT.QHS_12, DOCUMENT.QHS_13, DOCUMENT.QHS_14)
OBSArray = array("BG FASTING", "BG PC 1H BRK", "BG AC LUNCH", "BG PC 1H LUN", "BG AC DINNER", "BG PC 1H DIN", "BG BEDTIME")
avgOBSArray = array("BG FASTING A", "BG POSTBKFTA", "BG PRE-LNCHA", "BG POSTLNCHA", "BG PRE-DINRA", "BG POSTDINRA", "BG BEDTIME A")
matrixArray = array(fastArray, PCBArray, ACLArray, PCLArray, ACDArray, PCDArray, QHSArray)
/*Everything in the x loop (before and after the y loop) works on every column. I do all the count resets in the x loop because they only need to count up or down in each iteration of the y loop.*/
for x = 1, x <= size(matrixArray), x = x + 1 do
dateSubCount = 14
/*Everything in the y loop works for each field in the column*/
for y = 1, y <= 14, y = y + 1 do
/*OBSArray[y] keeps track of the position in the OBSArray array by using the value of x. str(matrixArray[x][y]) finds the value of the field at the x,y coordinates. str(dateSubCount) subtracts 1 on each loop to backdate the value to the correct date.*/
OBSNOW(OBSArray[x], str(matrixArray[x][y]), SUBTRACTDATES(str(._TODAYSDATE), "", "", str(dateSubCount)))
dateSubCount = dateSubCount - 1
/*avgSum is a total of every field in a column*/
avgSum = avgSum + matrixArray[x][y]
/*This checks to see if a field is filled in. If it is, fieldCount is incremented by 1. This will end up being the number avgSum is averaged by.*/
if matrixArray[x][y] "" then
fieldCount = fieldCount + 1
else
fieldCount = fieldCount
endif
endfor
/*We do this math and resets after the y loop is complete because we need the totals for that column. We reset avgSum and fieldCount here because they need to enter the next iteration of the x loop back at 0 to begin the process again.*/
if avgSum 0 and
fieldCount 0 then
finalAvg = avgSum/fieldCount
OBSNOW(avgOBSArray[x], str(round(finalAvg)))
else
""
endif
avgSum = 0
fieldCount = 0
endfor
}
}
Thanks again for your help. This was really fun to puzzle out.