This function finds each individual value in a comma separated values string.
global csv_start as dword
global csv_values as string
function csv$(called_values as string)
if len(called_values)
csv_values = called_values
csv_start = 1
exitfunction ""
endif
local length as dword
length = len(csv_values)
if csv_start > length then exitfunction "END OF STRING"
local finish as dword
local return_value as string
for finish = csv_start to length
if asc(mid$(csv_values, finish)) = asc(",")
return_value = right$(left$(csv_values, finish - 1), finish - csv_start)
csv_start = finish + 1
exitfunction return_value
endif
next finish
return_value = right$(csv_values, finish - csv_start)
csv_start = finish
endfunction return_value
To use it, call
csv$() with a string.
Then call it again in a loop with a null string and it will return each value one at a time. When there's no more values left, it returns "END OF STRING"
Here's an example:
csv_string as string
csv_string = "The,Game,Creators,Forum"
print "Before"
print
print csv_string
csv$(csv_string)
print
print "After"
repeat
csv_string = csv$("")
print csv_string
until csv_string = "END OF STRING"
wait key
end
global csv_start as dword
global csv_values as string
function csv$(called_values as string)
if len(called_values)
csv_values = called_values
csv_start = 1
exitfunction ""
endif
local length as dword
length = len(csv_values)
if csv_start > length then exitfunction "END OF STRING"
local finish as dword
local return_value as string
for finish = csv_start to length
if asc(mid$(csv_values, finish)) = asc(",")
return_value = right$(left$(csv_values, finish - 1), finish - csv_start)
csv_start = finish + 1
exitfunction return_value
endif
next finish
return_value = right$(csv_values, finish - csv_start)
csv_start = finish
endfunction return_value
[edit]
Optimized code.
Confucius Say...