I put together a complete working example of how to read a CSV file. It also includes a class to read and write an old Windows style INI configuration file.
I saved the program in Xojo Project format AND Xojo Binary format. I think the Binary format only works if you have a paid version of Xojo.
Things to watch out for:
I tested this on a MacBook (OSX) but I think you are using Windows. This should work fine but there might be some “quirks”.
Make sure the Copy File setting in the Build Settings in the IDE works. Those are found on the left column in the IDE and the Build Settings are on the bottom of that column. These copy a file (the sample INI file in this case) to where the executable file exists when it runs. You could put the INI file in some other directory and “hard code” where it was located. The code to read the INI file is in the APP.OPEN event and it currently assumes the INI file to be in the same directory as the executable. These don’t do anything useful for this example but I figured you might need them later. You can also examine them to help understand file reading/writing.
IMPORTANT - Find the System.DebugLog viewer on the bottom of the IDE. It is the little icon on the bottom of the IDE Window that looks like a WiFi symbol leaning to the right. There is code you will see in the program that displays stuff for debugging. VERY HANDY, especially when trying to learn something and see how it works.
This example lets you pick the desired CSV file from a popup dialog and defaults to looking for CSV files.
It assumes that your sample CSV file starts with the column names in row one. When you open the CSV file it loads the column names into an array. There is also another array you may not need but I used it to determine if the column was a string or a number. I loaded it from another source and that code is NOT in this sample program.
Once the CSV file is opened another button becomes active to load the data. It does not actually load anything but it does loop through the records and checks that the number of columns in the data rows match the number of columns in first row of names. When done it will display the record count or an error message if that happens.
I do some special processing of the delimiters in the CSV file with a convert function (NormalizeDelimiters) I wrote. I assume commas in input but that could be changed. I then convert each row when read to change the delimiter to an ASCII Chr(0). I observe quoted (“xxx”) fields and keep the comma delimiters inside of pairs of quotes but I remove the quotes on the row that is returned by the convert function. This avoids the whole problem of commas and quotes in data fields.
Take a look at the Array Append function. Look up Array in help (click the ? in the top of the Xojo IDE). Scroll to the bottom and click APPEND under the heading “See Also”. I don’t think Liberty BASIC has any equivalent and I have found it pretty handy in Xojo. It lets you add new elements to the end of an array without having to pre DIM the total size but I don’t think it works for multi-dimensional arrays.
Please dissect this program to understand it and don’t just use it and ignore how things are done. Read the WHOLE THING including the comments before you try any modifications.
I am a subscriber to xDev Magazine (Marc Zeedar’s post above) and the beginner article looks pretty useful. It has lots of info about the IDE in addition to the Xojo language itself. It would save you WAY over $10 in aspirin for headaches.
Eventually you will probably want to learn about SQLite for databases but for now see if you can take this skeleton and add some code to actually deal with the CSV file and do something useful. Remember you can put in lots of System.DebugLog statements to output messages and take “baby steps” to test your code. Make a few changes, print some stuff with System.DebugLog, and then repeat.
If you need more general help post here. If you have trouble with this example program also post here and I will try to help.
Fyi… the Binary format works for all License Levels… and is the ONLY one that can be WRITTEN in the “Free IDE” version. All versions can READ all the formats
OK … here are two more links. One for the Xojo binary project and one for a sample INI file. Both are NOT ZIPPED. You should be able to load the CSVframework.xojo_binary_project directly into the Xojo IDE. You can open the MyConfigSettings.ini sample file with notepad.
Thank you Mark. I managed to load it into Xojo and am looking through all the code. Going to take me a while to understand I’m sure. The csv files I access do not have column names in the first row. We have the data file map and code off of it.
Sure I am going to have questions. I’ll be back…LOL
By the way. I did go ahead and buy the xDev magazine with the Xojo beginners article.
Since you don’t have column names you can comment out that chunk of code in the FindTheFile method BUT you will also need to modify the data load method (LoadArray) since it assumes the number of names in the first row to be the number of columns in the data portion of the file. You can just modify the code like this:
'Current code
'Loop through the input row based on the number of columns found when
' the first row was processed by the FindTheFile method.
for i = 0 to ColumnNames.Ubound
'To this code since the ColCount is already calculated a few lines above here
'Loop through the input row based on the number of columns found in
' the data row just read.
for i = 0 to ColCount
I hope this helps you get this modified to work with your data.
One thing I noticed. You have file type and extension listed as CSV. But the files I use are extension jcp, made the change with no problem. But end users will access drf, or jcp or mcp depending on what data file provider they use. I tried to add all three in place of the csv and it didn’t work. How do I include all three extensions?