Author Topic: Split1000 (simple string parser) Collaboration  (Read 4716 times)

0 Members and 1 Guest are viewing this topic.

Offline The Librarian

  • Moderator
  • Newbie
  • Posts: 39
Split1000 (simple string parser) Collaboration
« on: August 12, 2019, 08:50:44 am »
Split1000 (simple string parser)

Contributor(s): @bplus, @luke, @SMcNeill
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=1073.0
Tags: [string] [parsing]

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

Source Code:
Code: QB64: [Select]
  1. _TITLE "Split demo 2" ' started by B+ on 08-27-2019 to compare to Item$ demo
  2. 'new data setup structure to streamline code in setup
  3.  
  4. SCREEN _NEWIMAGE(400, 300, 32)
  5. 'globals seen inside SUBs and FUNCTIONs because SHARED
  6. DIM SHARED topIndex AS INTEGER '<< this is how many keywords we have
  7. 'VVVV REDIM means dynamic arrays, so can change in setup
  8. REDIM SHARED d(1 TO topIndex) AS STRING
  9.  
  10. 'locals just seen in following main code section
  11. REDIM LIST$(1 TO 1)
  12. setup
  13. FOR i = LBOUND(d) TO UBOUND(d) 'all the keywords are first word$ of d() data array
  14.     Split d(i), ",", LIST$()
  15.     PRINT "Data index:"; i; " "; "String Item:"; LBOUND(LIST$); LIST$(LBOUND(LIST$))
  16.     FOR j = LBOUND(LIST$) + 1 TO UBOUND(LIST$)
  17.         PRINT SPACE$(15); "String Item:"; j; LIST$(j) 'j-1 counts off the items after the first
  18.     NEXT
  19.     PRINT: INPUT "Ok... press enter "; w$
  20.     CLS
  21.  
  22. SUB setup '3rd method of data structure, this is meant to be edited over and over as add to a refine words and substitutes
  23.     topIndex = 5 ' <<< make modifications to d() and then update this number, that's it!
  24.     REDIM d(1 TO topIndex) AS STRING
  25.     d(1) = "Months,January,February,March,April,May,June,July,August,September,October,November,December"
  26.     d(2) = "Days,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
  27.     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"
  28.     d(4) = "Test no further words/phrases in this line. (And the next test will do an empty string.)"
  29.     d(5) = ""
  30.  
  31. '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.
  32. 'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given.
  33. SUB Split (SplitMeString AS STRING, delim AS STRING, loadMeArray() AS STRING)
  34.     DIM curpos AS LONG, arrpos AS LONG, LD AS LONG, dpos AS LONG 'fix use the Lbound the array already has
  35.     curpos = 1: arrpos = LBOUND(loadMeArray): LD = LEN(delim)
  36.     dpos = INSTR(curpos, SplitMeString, delim)
  37.     DO UNTIL dpos = 0
  38.         loadMeArray(arrpos) = MID$(SplitMeString, curpos, dpos - curpos)
  39.         arrpos = arrpos + 1
  40.         IF arrpos > UBOUND(loadMeArray) THEN REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO UBOUND(loadMeArray) + 1000) AS STRING
  41.         curpos = dpos + LD
  42.         dpos = INSTR(curpos, SplitMeString, delim)
  43.     LOOP
  44.     loadMeArray(arrpos) = MID$(SplitMeString, curpos)
  45.     REDIM _PRESERVE loadMeArray(LBOUND(loadMeArray) TO arrpos) AS STRING 'get the ubound correct
  46.  

screenshot.PNG

Bplus Edit: bplus version was collaboration of @bplus, @luke, @SMcNeill URL where the original discussion was started by Luke contains his complementary Join function. The above is bplus version with a couple of revisions tweaked into it since first posting.
* Split.bas (Filesize: 2.7 KB, Downloads: 411)
« Last Edit: March 07, 2020, 02:41:43 pm by bplus »