Parsing a Delimited File
The following sample program can be used as a template for programs to parse delimited files, as well as for programs that create a database Lookup Table from the values in a delimited text file. (See the Sesame Programming Guide, or the Creating and Using a Lookup Table article elsewhere in the Knowledge Base.)
The example below opens a semicolon delimited file (myfile.txt), reads through it, and parses and distributes its values.
For this example, myfile.txt contains the following data:Tom;Bill;Jim;Edna Janice;Alec;Mark;Cliff Fred;Greg;Linda;Joanne;
The main program opens myfile.txt and sets the file position (FileSeek) to 0. A While loop is then started, its condition being that the file position (FilePos) is less than the length of the file (FileSize).
With each loop, the FileReadLn command reads the next line in the file, then the parseline subroutine is called.
Parseline prints the current line to the WriteLn window, then a For-Next loop parses the values in the line, distributing them into the six-element array named "t". Each parsed value is then individually written to the WriteLn() window. When finished, the WriteLn window displays the following:
Tom;Bill;Jim;Edna Tom Bill Jim Edna Janice;Alec;Mark;Cliff Janice Alec Mark Cliff Fred;Greg;Linda;Joanne Fred Greg Linda Joanne
See Importing Records with Programming Only (elsewhere in the Knowledge Base, or Appendix 2 of the Sesame Programming Guide) for a program that processes a delimited file and creates new records from it for the database.
/* Delimited text file parser sample program. Parses a semicolon-delimited text file named myfile.txt. Each line in the file expected to contain six values, in a format similar to any of these: Jim;Tom;Edna;Jane Jim,Tom,Edna,Jane Jim Tom Edna Jane Jim|Tom|Edna|Jane Set vDelim variable below to appropriate delimiter */ Subroutine parseline() var I as Int =1 WriteLn(vLine) While @Len(vLine) > 0 { t[i] = Split(vLine, vDelim) WriteLn(t[i]) i = i + 1 } End Subroutine vFileHandle = FileOpen("C:\Sesame\myfile.txt") If vFileHandle >= 0 { FileSeek(vFileHandle, 0) While FilePos(vFileHandle) < FileSize(vFileHandle) { FileReadLn(vFileHandle, vLine) If vLine <> "" Then { Parseline() } } FileClose(vFileHandle) } Else { WriteLn("Open failed: " + @Str(vFileHandle)) }
This, and other programming examples, can be found in Appendix 2 of the Sesame Programming Guide