The DATA function stores comma separated strings in an array which can be retrieved with the variable DATA$[]
FOR INTEGER i = 0 TO 4 PRINT DATA$[i] NEXT DATA one,two,three,"1,2,3,4",five
one two three 1,2,3,4 five
MACRO TheCount = UBOUND(DATA) ' <<<-- "DATA" must be in upper case PRINT "*************************************************" PRINT "There are", TheCount + 1, " strings in the DATA array ( 0 to 8 )." PRINT "*************************************************" FOR INTEGER i = 0 TO TheCount PRINT DATA$[i] NEXT DATA "111", "222", "333" DATA "444", "555", "666" DATA "777", "888", "999"
************************************************* There are 9 strings in the DATA array ( 0 to 8 ). ************************************************* 111 222 333 444 555 666 777 888 999
Stored data is accessed using the array variable DATA$[subscript]. The subscript index is a zero based literal or variable integer referring to the position of the data in the array. DATA functions can appear anywhere in the source code.
👉 Only one DATA array statement is permitted in a program. For a work around to this limitation see the example below following the READ$ section.
DATACOUNT returns the number of items contained in a DATA statement array.
Syntax:RetVal = DATACOUNT Return Value:
Parameters:
|
👉 The DATACOUNT and the DATA array both must exist in the same local scope of a SUB or FUNCTION including the MAIN or WINMAIN function.
$BCXVERSION "7.5.3" CALL Data_Foo SUB Data_Foo PRINT "There are", DATACOUNT, " data items in this SUB" FOR INT i = 1 TO DATACOUNT PRINT READ$(i) NEXT DATA one, two, three, four, five END SUB
There are 5 data items in this SUB one two three four five
READ$ returns the string located at a specified position in a DATA array.
Syntax:RetStr = READ$(DATAPosition AS INTEGER) Return Value:
Parameters: |
DIM Accumulator AS DOUBLE DIM Ndx = 1 WHILE READ$(Ndx) <> -1 INCR Accumulator, READ$(Ndx++) WEND PRINT Accumulator ' Result = 45.9 DATA 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, -1
45.9
👉 Only one DATA array statement is permitted in a program. Here is a work around to this limitation.
'************************************************************* ' Here are two functions and a demo that shows how to define ' and use multiple local DATA sets using the READ$ function. '************************************************************* FUNCTION MONTH_NAME$ (i) SET DATA[] AS LPCTSTR ' DATA must precede CODE that references it. "January","February","March","April", "May","June","July","August", "September","October","November","December" END SET IF i < 1 OR i > 12 THEN EXIT FUNCTION ' Return an empty string FUNCTION = READ$(i) ' READ$() works well here too. END FUNCTION FUNCTION WEEKDAY_NAME$ (i) SET DATA[] AS LPCTSTR "Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday" END SET IF i < 1 OR i > 7 THEN EXIT FUNCTION FUNCTION = READ$(i) END FUNCTION '--------------------------------------------- ' -- Begin Demo -- '--------------------------------------------- CLS PRINT "Here are the MONTH NAMES:" PRINT FOR INT i = 1 TO 12 PRINT MONTH_NAME$(i) NEXT PRINT PRINT "Here are the WEEKDAY NAMES:" PRINT FOR INT i = 1 TO 7 PRINT WEEKDAY_NAME$(i) NEXT PAUSE
Here are the MONTH NAMES: January February March April May June July August September October November December Here are the WEEKDAY NAMES: Sunday Monday Tuesday Wednesday Thursday Friday Saturday Press any key to continue . . .
Whereas READ$ exists to read strings from DATA statements, READ exists to read literal numbers, including integers, single and double precision, and scientific notation. Some compilers will also accept HEX numbers in the form 0x and 0X. MSVC, CLang, and Pelles do, while MinGW, Embarcadero, and LccWin32 do not, at this time.
Syntax:RetVal = READ(DATAPosition AS INTEGER) Return Value:
Parameters: |
DIM Accumulator AS DOUBLE DIM Ndx = 1 WHILE READ(Ndx) <> -1 INCR Accumulator, READ(Ndx++) WEND PRINT Accumulator DATA 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, -1
45.9