OPTION _EXPLICIT
_TITLE "Split demo 2" ' started by B+ on 08-27-2019 to compare to Item$ demo
'new data setup structure to streamline code in setup

SCREEN _NEWIMAGE(400, 300, 32)
'globals seen inside SUBs and FUNCTIONs because SHARED
DIM SHARED topIndex AS INTEGER '<< this is how many keywords we have
'VVVV REDIM means dynamic arrays, so can change in setup
REDIM SHARED d(1 TO topIndex) AS STRING

'locals just seen in following main code section
DIM i AS INTEGER, j AS INTEGER, w$
REDIM LIST$(1 TO 1)
setup
FOR i = LBOUND(d) TO UBOUND(d) 'all the keywords are first word$ of d() data array
    Split d(i), ",", LIST$()
    PRINT "Data index:"; i; " "; "String Item:"; LBOUND(LIST$); LIST$(LBOUND(LIST$))
    FOR j = LBOUND(LIST$) + 1 TO UBOUND(LIST$)
        PRINT SPACE$(15); "String Item:"; j; LIST$(j) 'j-1 counts off the items after the first
    NEXT
    PRINT: INPUT "Ok... press enter "; w$
    CLS
NEXT

SUB setup '3rd method of data structure, this is meant to be edited over and over as add to a refine words and substitutes
    topIndex = 5 ' <<< make modifications to d() and then update this number, that's it!
    REDIM d(1 TO topIndex) AS STRING
    d(1) = "Months,January,February,March,April,May,June,July,August,September,October,November,December"
    d(2) = "Days,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
    d(3) = "Holidays,NewYears Day,MLK Day,Valentine's Day,Easter,Mother's Day,Memorial Day,Father's Day,Independence Day,Bplus Day,Labor Day,Halloween,Thanksgiving,Christmas"
    d(4) = "Test no further words/phrases in this line. (And the next test will do an empty string.)"
    d(5) = ""
END SUB

'This SUB will take a given N delimited string, and delimiter$ and creates an array of N+1 strings using the LBOUND of the given dynamic array to load.
'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given.
SUB Split (SplitMeString AS STRING, delim AS STRING, loadMeArray() AS STRING)
    DIM curpos AS LONG, arrpos AS LONG, LD AS LONG, dpos AS LONG 'fix use the Lbound the array already has
    curpos = 1: arrpos = LBOUND(loadMeArray): LD = LEN(delim)
    dpos = INSTR(curpos, SplitMeString, delim)
    DO UNTIL dpos = 0
        loadMeArray(arrpos) = MID$(SplitMeString, curpos, dpos - curpos)
        arrpos = arrpos + 1
        IF arrpos > UBOUND(loadMeArray) THEN REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO UBOUND(loadMeArray) + 1000) AS STRING
        curpos = dpos + LD
        dpos = INSTR(curpos, SplitMeString, delim)
    LOOP
    loadMeArray(arrpos) = MID$(SplitMeString, curpos)
    REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
END SUB



