[quote=188755:@Norman Palardy]Ditto
Used to write PL/1, Adabas/Natural, some Cobol, Fortran and a whole lot of other non-UI procedural code
Once you get it it just clicks
Stick with it[/quote]
My husband Richard used to be a mainframe programmer and used everything that you have mentioned.
Emile, I have looked far and wide. But the code in examples here is so cumbersome and not clear at all, at least to me. In LB , my former language, reading csv files is so easy. They have an Inputto function that reads each cell in a column which in the files I’m using are all strings.
Maybe this could help you with the reading and displaying of CSV data:
Dim f As FolderItem
Dim tis As TextInputStream
Dim col() As String
f = // create FolderItem to your file
tis = TextInputStream.Open(f)
while not tis.EOF
col = Split(tis.ReadLine(), ",")
Listbox1.AddRow(col)
wend
tis.Close
Thanks buddy. I’ll give it a try. In my files there are no headers and I know the exact numbers of coloumns in each file as they are constant from file to file.
Btw, I don’t need to display the info that I read from file, I need to put it in a container of some sort to hold until I need to access the info to do some calculations. I could use some help with that. And some of my data has a " , " in it and not meant to be a separator in that instance.
There are plenty of ways you could store the CSV data, and I suppose it will depend a lot on how you want to use the data.
One of my favorite approaches is to parse all data into a JSONItem object (container). Since JSON consists of key:value pairs (dictionaries) and arrays, you can represent any kind of data with it.
Not sure how you would go about the additional “,” in the lines. Are certain fields rapped in quotes (e.g. “some , field”, 1, 3)? My simplistic example obviously won’t work on this kind of data.
Dim f As FolderItem
Dim tis As TextInputStream
Dim col() As String
f = App.ExecutableFile.Parent.Parent.Child("test.csv")
tis = TextInputStream.Open(f)
while not tis.EOF
col = SplitCSV(tis.ReadLine())
Listbox1.AddRow(col)
wend
tis.Close
You’ll notice the Split call is replaced with a SplitCSV call. This is a custom function I’ve quickly put together to parse CSV lines like the one you described above. There is an added benefit that you can mix strings and numbers in your lines. (e.g. “str”, 2, “abc”). It will however NOT work with UNICODE strings.
Function SplitCSV(source As String) As String()
Dim mb As MemoryBlock
Dim i As Integer
Dim token() As String
Dim result() As String
Dim inString As Boolean
mb = source
inString = false
i = 0
while i < mb.Size
if inString then
// quote?
if mb.Byte(i) = 34 then
inString = false
result.Append Join(token, "")
redim token(-1)
while (i < mb.Size) and (mb.Byte(i) <> 44)
i = i +1
wend
else
token.Append Chr(mb.Byte(i))
end if
else
select case mb.Byte(i)
case 44 // comma
result.Append Join(token, "")
redim token(-1)
case else
if mb.Byte(i) = 34 then
inString = true
else
token.Append Chr(mb.Byte(i))
end if
end select
end if
i = i + 1
wend
if token.Ubound >= 0 then
result.Append Join(token, "")
end if
return result
End Function
You’re welcome Milfredo. I actually also need to do CSV parsing in one of my upcoming projects. So you had good timing with your question I’ll be using some the routines in my project as well.
Just take note, the routine does not do any kind of error checking on malformed CSV data.
Ok guys. I spent 3 hours going through the manuals and the Introduction book. I have a simple text file in my documents folder. I am trying to open it and read it into a ListBox. I get exactly what I need it to do as long as I give the user the opportunity to go and select the file. Works perfectly. Problem is, this is a file that needs to be read automatically and the contents of the file put in the Listbox. Every example I could find has code for a user dialog box to open. But that is not what I want or need.
I need to populate this listbox when it opens up in a screen so the user can see all the data. Then I will let him do something with the data. Please help. I have tried reading, researching looking at examples. I can not figure it out.
I just need to clarify this. The storage mechanism you choose for storing data in memory depends largely on the size, type and requirements for using the data. If you need to do high performance calculations on your data then you will have to look at a binary format, and NOT use JSON for in-memory storage. Using a binary format do require more effort to begin with, but is worth it if speed is your need.
The convenience that JSON provides as an in-memory storage format, should only be limited to very small sets of data. For example, I use JSON often to store the settings of my application.
Can someone tell me what’s wrong with this code.
On run the program stops and shows bug next to this line: textInput =TextInputStream.Open(f)
When I use this: f=GetOpenFolderItem(“tracks”) there is no bug, it works perfectly. But I don’t want user choosing a file at this point.
Dim f As FolderItem
Dim textInput As TextInputStream
Dim col() As String
f = SpecialFolder.Documents.Child(“tracks”)
//f=GetOpenFolderItem(“tracks”)// create FolderItem to your file
If f <> Nil Then
textInput =TextInputStream.Open(f)
else
MsgBox(“No File Found”)
End IF
while not textInput.EOF
col = Split(textInput.ReadLine(), “,”)
TracksListbox.AddRow(col)
wend
The file doesn’t exist. Is it named simply “tracks”, with no extension? Use FolderItem.Exists:
if f <> nil and f.Exists then
f = nil means the file path is bad. f <> nil means that the folderitem could represent an actual file on disk in a location that it would be possible to create it if it didn’t already exist. f.Exists = True means the file is actually there.