Advanced array processing Tutorial
In the first tutorial we introduced you to arrays and to the MAT keyword. Arrays can be used in a variety of ways. Here we will cover a few more ways to handle them.
STR2MAT
STR2MAT allows the program to change strings directly into arrays as specified in the statement. 1. The syntax is STR2MAT(“string”,mat xxx$,“delimiter”) The delimiter can be anything you choose, including a comma, a space, or any letter or symbol. For example:
00010 STR2MAT(“Dallas Houston Amarillo Austin”,mat city$, “ “) 00020 print mat city$
will return:
Dallas
Houston
Amarillo
Austin
This could make putting information into an array much easier and faster.
It's opposite, MAT2STR, will take a matrix and change it into a string. It's syntax is:
MAT2STR(mat xxx$,stringname$,”delimiter”)
For example:
MAT2STR(mat city$,citynames$,”::”)
will return Dallas::Houston::Amarillo::Austin
A note about variables. STR2MAT and MAT2STR can work if the entire string part is a variable, i.e.:
a$=(“hot,warm,mild,cool,cold”) str2mat(a$,mat weather$,”,”)
but it can't handle variables, as in:
a$=”cold” b$=”hot” str2mat(a$,b$,mat weather$,”,”) ! doesn't work!
For a more practical example, open your sample program SAMPLIST and alter the data lines at the top to include STR2MAT. Make sure your program still runs! See the solutions page for the answer.
SRCH
Srch provides for a quick and easy search of an array and returns the row number where it finds the matching argument. Here's the syntax for both string and numeric arrays:
Srch(array-name$,^argument$[,row])
for string arrays and
Srch(array-name,argument[,row])
for numeric arrays.
The [row] parameter is optional and provides the starting row number for the search. The carrot (^) symbol can precede the argument$ parameter. It's not included in the search, but makes argument$ case insensitive.
Write a sample program, accessing our data file ORDERS.int, to find the customers buying all three items? (They'll be getting a special 10% discount!) Use a DO LOOP. See the solutions page for help.
Note: if a match is not found, Srch will return -1.
Next, what if you wanted to organize the names in alphabetical order?
AIDX/DIDX
3. AIDX and DIDX are keywords used for creating an index for an array by ascending (AIDX) or descending (DIDX) order. The index can be used as subscripts to access the values of the array in sorted order (for both numeric and string arrays).
The syntax is as follows:
MAT AscendingIndexArray(UDIM(ArrayToBeIndexed$))=AIDX(ArrayToBeIndexed$)
or
MAT DescendingIndexArray(UDIM(ArrayToBeIndexed$))=DIDX(ArrayToBeIndexed$)
For example, if you wanted to take your customer list and put them in alphabetical order by first name, you could do this (after opening the file and reading the names into arrays):
let y2=udim(firstname$) mat alphanames(y2)=aidx(firstname$) for n=1 to y2 print firstname$(alphanames(n));lastname$(alphanames(n)) next n
These can be used with numeric values as well. Here are the monthly totals for several customers' purchases, and you want to reward the highest spenders. List them in descending order
300,500,66,789,1023,24,56,72,800,945,15,7
See solutions for help.
Next: BRConfig.sys Tutorial
Back: Table of Contents