/*
 * This is a sample script which is called with the "Stats" item,
 * installed using the $MENU specification in the input mask
 *
 * It shows how to control AFile using AREXX
 *
 * This script counts how many disks have been purchased each year
 */

SIGNAL ON ERROR
OPTIONS RESULTS
ADDRESS "AFile_rexx"

/* reset variables */
Year  = 0
Count = 0
SAY "Number of disks purchased by year" || D2C(10)

/* sort the file on purchase date (ascending order) */
"SORT PURCHASE"

/* process all the records */
"SEEK FIRST"
DO FOREVER

    /* get year of purchase in current record */
    "GETFIELD PURCHASE"
    NewYear = RIGHT( RESULT , 2 )

    /* compare with previous year */
    IF NewYear ~= Year THEN DO
	CALL DisplayResult( Year Count )
	Count = 0
    END

    /* update count */
    Year  = NewYear
    Count = Count + 1

    /* read next record */
    "SEEK NEXT"
END

/*
 * We should come here only because "SEEK NEXT" has returned an error
 * (end of file)
 */

ERROR:

/* flush last result */
CALL DisplayResult( Year Count )

/* release sort */
"RELEASE ALL"

EXIT 0

/*
 * Procedure called to display the result for one year
 */

DisplayResult: PROCEDURE
	PARSE ARG Year Count

	IF Year ~= 0 THEN DO
		SAY "19" || Year || " : " || Count
	END

	RETURN "0"

