This code tokenizes a comma separated values string.

Call Csv_addval() with the string you want to tokenize
Csv_getval() returns a token (this returns "CSV_EMPTY" when there are no tokens left)
Call Csv_reset() to clear and tokens left in memory

Note: If you have the extends plugin you can replace the call to "str_find_char(Csv_str, ",")" with "str find char(Csv_str, ",", 1)" and delete the str_find_char() function.

Language: 
Dark Basic Pro
Code: 
global Csv_str as string

`adds one or more values to the csv system
function Csv_addval(str as string)

  if len(Csv_str)
    `add a preceding comma if there already are values
    str = "," + str
  endif

  Csv_str = Csv_str + str

endfunction

`retrives a value from the csv system
function Csv_getval()

  local value as string
  local firstcomma as dword

  `return "Csv_EMPTY" if there are no values left
  if len(Csv_str) = 0 then exitfunction "CSV_EMPTY"

  firstcomma = str_find_char(Csv_str, ",")
  if firstcomma
    `there is more than one value, so return everything left of the comma
    value = left$(Csv_str, firstcomma-1)
    Csv_str = right$(Csv_str, len(Csv_str) - firstcomma - 1)
  else
    `this is the last value, so return all that's left
    value = Csv_str
    Csv_reset()
  endif

  value = trim(value)

endfunction value

`resets the csv system
function Csv_reset()

  Csv_str = ""

endfunction

`find a character
`this is the same as str find char in the extends plugin
`besides the fact that it doesn't use the counter parameter
function str_find_char(str as string, char as string)

  local t as dword

  for t = 1 to len(char)
    if mid$(str, t) = char then exitfunction t
  next t

endfunction 0
3.5
Average: 3.5 (2 votes)