Build and fill in an array then retrieve data from it

Posted by

Sometimes you will need to temporarily store data, before aggregation, cumul, re-order, etc… An array is the solution to perform this in memory (e.g. no physical write so very fast and no I/O). You may also use such memory-stored data for any purpose (produce an a-mail, a report, etc…). To achieve this, I’ve split the tasks in several steps:

1. Main ARRAY initialization, to store any content
ARRAY.NAME = ''
2. Give names to fields in order to clarify array content
EQUATE
    FIELD.NAME.a TO 1,  
    FIELD.NAME.b TO 2,  
    FIELD.NAME.c TO 3,  
    FIELD.NAME.d TO 4,  
    FIELD.NAME.e TO 5
* associate column positions to a name (FIELD.NAME.x) helps to identify the "business meaning" of each column of the array in the below code
3. Then inside a loop, populate the array
	MAIN LOOP
		FOR I = 1 TO 10
				GOSUB BUILD.ARRAY
				GOSUB EXTRACT.DETAILS
		NEXT I
*-----------------------
BUILD.ARRAY:
*-----------------------
*
        LOCATE ACCT.OFF IN ARRAY.NAME<FIELDNAME.a,1> BY 'AR' SETTING POS ELSE POS = ''
        IF POS = '' THEN
           INS ACCT.OFF BEFORE ARRAY.NAME<FIELD.NAME.a,POS>  
           INS ANY.VARIABLE.2<2,I> BEFORE ARRAY.NAME<FIELD.NAME.b,POS>  
           INS ANY.VARIABLE.3<3,I> BEFORE ARRAY.NAME<FIELD.NAME.c,POS>  
           INS ANY.VARIABLE.4<4,I> BEFORE ARRAY.NAME<FIELD.NAME.d,POS> 
           INS ANY.VARIABLE.5<5,I> BEFORE ARRAY.NAME<FIELD.NAME.e,POS>   
        END
        RETURN
* BY 'AR' means data will be sorted ascending in the array. See below all other options.
4a. Extract the array content (For/Next loop) 
*----------------
EXTRACT.DETAILS:
*----------------
       NB.ITEMS = DCOUNT(ARRAY.NAME<FIELD.NAME.1>,@FM)

   FOR I = 1 TO NB.ITEMS
        EXTRACTED.FIELD.1 = ARRAY.NAME<FIELD.NAME.1,I>
        EXTRACTED.FIELD.2 = ARRAY.NAME<FIELD.NAME.2,I>
        EXTRACTED.FIELD.3 = ARRAY.NAME<FIELD.NAME.3,I>
        EXTRACTED.FIELD.4 = ARRAY.NAME<FIELD.NAME.4,I>
        EXTRACTED.FIELD.5 = ARRAY.NAME<FIELD.NAME.5,I>
   NEXT I
  • NB: For the LOCATE, you can use:

AL Values are in ascending alphanumeric order
AR Values are in right justified, then ascending order
AN Values are in ascending numeric order
DL Values are in descending alphanumeric order
DR Values are in right justified, then descending order
DN Values are in descending numeric order

4b. Extract the array content (Loop/Remove)

An alternative way to retrieve data from an array is REMOVE. Used usually in a loop, it automatically browse the content of an array and extract data for each separator found (FM, VM, SM). After each single data extracted, a pointer (in memory) is moved to the next separator until the last one found in the array.

* Example: YR.RECORD= A <VM> A1 <VM> A2 <FM> B <FM> C <FM> D <FM> E
* (so an array mixing FM and VM separators)

YR.KEY = ''
LOOP
REMOVE YR.KEY FROM YR.RECORD SETTING TAG
WHILE YR.KEY : TAG DO
YT += 1
CRT YT : " YR.KEY= " : YR.KEY
REPEAT
Running the above code will gives:
1 YR.KEY= A
2 YR.KEY= A1
3 YR.KEY= A2
4 YR.KEY= B
5 YR.KEY= C
6 YR.KEY= D
7 YR.KEY= E

Note that after the last data extracted, the pointer in memory remains at the last separator found. If you need to re-extract data from the same array using a REMOVE, you have to re-initialize the pointer by setting this:
YR.RECORD = YR.RECORD
A further REMOVE from YR.RECORD will then start from the first separator found.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.