How many times did you try to load a csv data file? Those files with a comma (or other char) delimited fields? How many For/Next loops did you use?
What about just few lines of code to do the job? Here an example and the CSV sample data is attached.
This script has more than few lines just to show you in details the process but all the job is done by the following single line:
[code=thinbasic]
PARSE(FILE_Load(FileToLoad), MyMatrix(), $crlf , ",")
[/code]
_________________________________________________
[code=thinbasic]
uses "file"
uses "console"
dim FileToLoad as string value app_sourcepath & "CSV_SampleData.csv"
dim MyMatrix() as string
dim nLines as long
dim nCols as long
dim T0, T1 as double
console_writeline("This script demonstrate the use of PARSE function for loading, parsing and filling a string array with the content of a CSV file. All with only one line of code.")
console_writeline("Input file: " & FileToLoad)
console_writeline("File size : " & file_size(FileToLoad) & " bytes")
console_writeline("Press any key to start.")
console_writeline(repeat$(79, "-"))
console_waitkey
'---
'---Starting to load and parse input file
'------
T0 = timer
'---
'---Just one line do the job of loading file data, parsing text lines, dimensioning and filling the matrix.
'------
PARSE(FILE_Load(FileToLoad), MyMatrix(), $crlf , ",")
'--Now get the number of lines and max number of columns parsed
nLines = ubound(MyMatrix(1))
nCols = ubound(MyMatrix(2))
T1 = timer
'---Write some info
console_writeline("Total loading and parsing time: " & format$(T1 - T0, "#0.000000") & " secs")
console_writeline("Total number of lines : " & nLines)
console_writeline("Total number of colums: " & nCols)
console_writeline("Press any key to show result on screen")
console_writeline(repeat$(79, "-"))
console_waitkey
dim CountLine as long
dim CountCol as long
for CountLine = 1 to nLines
for CountCol = 1 to nCols
console_write(MyMatrix(CountLine, CountCol) & " ")
next
console_writeline("")
next
console_writeline(repeat$(79, "-"))
console_writeline("Program terminated. Press any key to close.")
console_waitkey
[/code]
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
This is very very useful Eros, thanks!!
Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server
Bookmarks