in

Using array to condition looping through another array

I need to create a report to summarize x fields in a table row, based on selection of account, profit center and month/months.  The table is GL master and has a row for each account, year, profit center.  The row has 12 fields that hold the total $ , one for each month.
Created parameter selection, one is static for months, 1-12.
Array created with the fields for the row that hold the $ for each month, called "P".
Array created based on the parameter for the months selected.  It works, and for instance if the user selects month 3,4,5 those values are in the array "M".
I want to take those values from the array Months and use it to select the corresponding value fields from array P that correspond to those positions.  So for M[1] (value is 3) in this case I want to select P[3].  Is there a way to do this with a for loop.  I can count the # of values in the array M, but how do I substitute the value for my selection from array P??  I just on't know much about the syntax for for loops, but it seems this would be do-able.

Solution: Using array to condition looping through another array

So, you're saying that the months that the user selected (eg. 3, 4 and 5) are stored in array M?  (That's not the way I read things at first, but now I think that's what you're saying)

 If so, and you want to use those values to get the corresponding entry from array P, the basic form would be P [ M [ 1 ] ].  The question is, what do you want to do with those values?

 If you just want to show them individually on the report, I don't think a loop is necessary (or helpful).  You'd probably have 12 formulas with P [ M [ 1] ], P [ M [ 2 ] ], etc.  Technically, you'd need to check for a 0 subscript, so the formulas would be like:

if M [ 1 ] > 0 then
  P [ M [ 1 ] ]

 Same thing for 2 - 12.

 OTOH, if you want to add the values for those months together to get a total, then a loop would make sense.  Something like:

(declare your arrays)
Local NumberVar i;
Local NumberVar total;

for i := 1 to 12 do
  if M [ i ] > 0 then
    total := total + P [ M [ i ] ]
  else
//  exit the loop when we run out of values in M
    exit for;

total


 James