DATA statement

Purpose:

The DATA function stores comma separated strings in an array which can be retrieved with the variable DATA$[]

Example 1:

FOR INTEGER i = 0 TO 4
 PRINT DATA$[i]
NEXT

DATA one,two,three,"1,2,3,4",five

Result:

one
two
three
1,2,3,4
five

Example 2:

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"

Result:

*************************************************
There are 9 strings in the DATA array ( 0 to 8 ).
*************************************************
111
222
333
444
555
666
777
888
999

Remarks:

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 function

Purpose:

DATACOUNT returns the number of items contained in a DATA statement array.

Syntax:

RetVal = DATACOUNT

Return Value:

  • Data type: INTEGER
    RetVal Number of items contained in the DATA array.

Parameters:

  • None

Remarks:

👉 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.

Example:

$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

Result:

There are 5 data items in this SUB
one
two
three
four
five

BCX Console Sample Programs using the DATA function.


READ$ function

Purpose:

READ$ returns the string located at a specified position in a DATA array.

Syntax:

RetStr = READ$(DATAPosition AS INTEGER)

Return Value:

  • Data type: STRING
    RetStr The string located at the position specified in the DATA array.

Parameters:

  • Data type: INTEGER
    DATAPosition The position in the DATA array, is 1 based, meaning that
    RetStr = READ$(2)
    
    would return "oranges" from the array
    DATA "apples", "oranges", "pears"
    

Example:

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

Result:

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

Result:

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 . . .

READ function

Purpose:

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:

  • Data type: DOUBLE
    RetVal The numerical value located at the position specified in the DATA array.

Parameters:

  • Data type: INTEGER
    DATAPosition The position in the DATA array, is 1 based, meaning that
    RetVal = READ(2)
    
    would return the numerical value 283 from the array
    DATA 265, 283, 327
    

Example:

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

Result:

45.9