Trying to understand Xojo

[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.

I will continue to search for an answer.

Thanks for the info,
Milfredo

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

This reads the following CSV data from the file:

a,b,c,d
e,f,g,i
1,2,3,4

and displays it as separate cells in a listbox:

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.

Milfredo

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.

Alwyn,
All fields that are not empty are enclosed in quotes as they are all strings.

Hey Milford,

You can try this algorithm:

  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

For this CSV data:

"a",,,"d"
"e",f,"g",
"1",,"3",4
"some , commas",123,"abc",5

I get the following result:

Thank you.

I will give it a try and see how it goes. I really appreciate it.

Milfredo

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 :wink: 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. I have error checking in my other program, hopefully when I get around to the csv coding I will be able to add it.

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.

Thanks,
Milfredo

You can instantiate a new FolderItem object directly with the path, and the use the object wherever you need it.

f = new FolderItem("C:\\someFolder\\myFile.txt")

You can also use SpecialFolder to get at certain predefined locations without having to resort to a fixed path.

UPDATE:

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.

I’ll look into that. I don’t need lots of storage and it will be temporary anyway.

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

textInput.Close

Thanks.

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.

Thanks Tim. The .txt was missing but no where was that apparent in the errors…

Thanks again,

Milfredo

Code works now…YEA!