mvBase stores all data as a string. File items are separated by segment marks, and lines in file items are separated by attribute marks. For processing data in file items, therefore, mvBASIC supplies several powerful string functions. Using these functions, fields in a file item can be distinguished and processed separately.
There are two categories of string function: those which require a delimiter to be specified, and those which assume the dynamic array delimiters.
Dynamic arrays are a powerful data structure in mvBASIC, since they can be used to represent the contents of a file item. A dynamic array is simply a string variable with attribute marks, value marks, and subvalue marks taken to be field delimiters.
When a file item is read into a string variable by a READ or READU statement, the fields are separated by attribute marks (CTRL+^, or CHAR(254)), and subfields are generally separated by value marks and subvalue marks (CTRL+] and CTRL+\ , or CHAR(253) and CHAR(252)). The string variable is thus in the form of a dynamic array and can be manipulated by the dynamic array functions.
NOTE |
When these delimiters are sent to the screen via a PRINT or CRT statement, they will not appear as expected: mvBASIC will subtract 127 from the ASCII value of a high-order character on output. Thus CHAR(254) will appear on output as ~, CHAR(253) will appear as }, and CHAR(252) will appear as |. |
See Reading and Updating File Items later in this section for more information on reading file items into an mvBASIC program.
To examine or alter the contents of a particular attribute, value, or subvalue of a dynamic array, mvBASIC provides the EXTRACT, REPLACE, INSERT, and DELETE functions.
The EXTRACT function returns the contents of the particular attribute, value, or subvalue. For example, if Attribute 6 of the dynamic array CUST contains the customer’s zip code, a variable ZIP can be assigned with:
ZIP = EXTRACT(CUST,6) |
The REPLACE function replaces the contents with new data. For example, if a customer had a new zip code NEW.ZIP, it can replace the old zip code in Attribute 6 of the array CUST with:
CUST = REPLACE(CUST, 6 ; NEW.ZIP) |
The INSERT function inserts data as an attribute, value, or subvalue in the given position. For example, if Attribute 6 of CUST does not exist, it can be assigned to the customer’s zip code ZIP with:
CUST = INSERT(CUST , 6 ; ZIP) |
The difference between REPLACE and INSERT is that REPLACE will overwrite any data already in the given position, but INSERT will simply move it up. If Attribute 6 had already existed in the previous example, the new data will become Attribute 6, the old Attribute 6 will become Attribute 7, and so on.
The DELETE function deletes the specified attribute, value, or subvalue. For example, Attribute 7 of CUST can be deleted with:
CUST = DELETE(CUST , 7) |
The DELETE function does not perform the same function as using REPLACE with the null string. By deleting Attribute 7, Attribute 8 becomes Attribute 7, and so on. By replacing Attribute 7 with the null string, Attribute 7 becomes null and all other attributes remain unchanged.
In addition to the four functions discussed in the preceding section, the LOCATE statement proves to be extremely powerful in manipulating dynamic arrays. The LOCATE statement searches for a particular attribute, value, or subvalue within a dynamic array string (or subset thereof). If the data has been sorted into an ascending or descending order, the order can be specified in the LOCATE statement. THEN and ELSE clauses are accepted by LOCATE to specify action if the string is or is not found.
If the string is found, the LOCATE statement sets a specified variable to the position where the data was found, and the statements of the THEN clause, if included, are executed. If the string is not found where expected, the variable is set to the current position plus one, and the statements in the ELSE clause are executed. In the ELSE clause, the variable can be used with an INSERT function to place the data in the proper position.
For example, if a dynamic array LIST contains names in alphabetical order separated by attribute marks, a new name NAME can be inserted with:
LOCATE(NAME, LIST; POSITION; "A") THEN PRINT NAME : " ALREADY LISTED." END ELSE NAMELIST = INSERT(LIST , POSITION; NAME) END |
An enhancement of mvBASIC is an alternate form for each of the dynamic array processing functions. This form uses angle brackets in referencing a dynamic array field, thus simulating the syntax for referencing dimensioned arrays. Since the angle brackets tend to be more intuitive, they are generally preferred over the older syntax forms.
The preceding example lines might have read:
Old |
New |
ZIP = EXTRACT(CUST,6) |
ZIP = CUST<6> |
CUST = REPLACE(CUST, 6 ; NEW.ZIP) |
CUST<6> = ZIP |
CUST = INSERT(CUST , 6 ; ZIP) |
INS ZIP BEFORE CUST<6> |
CUST = DELETE(CUST , 7) |
DEL CUST<7> |
LOCATE(NAME, LIST; POSITION; "AL")... |
LOCATE NAME IN LIST BY "AL"... |
The COUNT function returns the number of times a specified substring appears in a string. The function returns zero if the substring is not found. If the substring is the null string, the function returns the number of characters in the string minus one.
The DCOUNT function returns the number of fields separated by a given delimiter. DCOUNT can be very useful for processing dynamic arrays as well as other strings. For example, the number of attributes in a string ADDRESSES can be determined with:
NO.OF.ATTRS = DCOUNT(ADDRESSES, CHAR(254)) |
See Also
Overview of mvBASIC Statements and Functions
Sending Output to the Screen and Printer
Reading and Updating File Items
Reading and Writing Tapes or Floppy Disks