EOF .line csv mischief

Ask about the individual Gambas components here.
Post Reply
tailkinker
Newbie
Posts: 32
Joined: Tue Sep 17, 2024 11:06 am

EOF .line csv mischief

Post by tailkinker »

I am trying to find out how many lines are in my csv file. It keeps coming back as 0, even though there are 5 lines plus the fields in the file according to Geany.

The code I am using is

dim addline as integer

addline = writer.line

This is also happening with the test.csv file that I created for reading. If I try to use eof - it doesn't work on either of my files, btw. If I use a for/next with a number before what eof would be, then it reads fine.

Lucille
BruceSteers
Legend
Posts: 2081
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: More csv mischief

Post by BruceSteers »

Trouble with CsvFile.class is it is inherited by Stream.class

Meaning it basically works like file stream access only. (Open/Read/etc)

There is not a Count property to tell how many lines there are as the file will not have been read yet.

.Line denotes what line you are on during Read methods.

The only way to tell how many total lines is to read them all into an array until Eof and then count the array.

After your last questions i went about making a CsvObject.class

Check out the attached project.

It implements a CsvObject.class

CsvObject.class uses CsvFile.class to load a file into an array.
You can then use the array.

it has the following properties..

Property Read Path As String Use $sPath
'' Get or set the Fileds
Property Fields As String[] Use $aFields
'' Array of Collections of each item
Property Items As Collection[] Use $aItems

Property Read Count As Integer
Property Read Max As Integer

'' CsvFile.class related.
Property KeepNames As Boolean Use $bKeepNames
Property Separator As String Use $sSeparator
Property Escape As String Use $sEscape
Property CharSet As String Use $sCharSet


It also has the following methods...

'' Add items as a csv string, a String[] array or as a Collection (Same as CsvFile.Write\())
Public Sub Add(vLine As Variant, Optional Index As Integer = -1)

'' Search for an item, default field is the first one but can be changed.\
'' Returns the position in the Items array or -1 if not found.
Public Sub Find(Search As String, Optional Field As String = $aFields[0]) As Integer

'' Uses CsvFile to load a file into a CsvObject.
Public Sub Load(Path As String, Optional (Separator) As String, Escape As String, CharSet As String)

'' Save the file
Public Sub Save(Optional Path As String)

It also automatically adds the CsvFile.AppendCsv() method to CsvFile.class like was discussed in your previous question.


Maybe you can find it helpful to make your own csv object manager?

some inline help has bee added
You do not have the required permissions to view the files attached to this post.
tailkinker
Newbie
Posts: 32
Joined: Tue Sep 17, 2024 11:06 am

Re: EOF .line csv mischief

Post by tailkinker »

Bruce,

I was hoping you would take pity on me and write something like this.

I've been trying to figure out how to do something similar.

Will let you know how this works.

PS - this is what I've been working on, and where it is giving me an error message.

Lucille
Screenshot at 2025-02-28 10-31-49.png
Screenshot at 2025-02-28 10-33-47.png
Screenshot at 2025-02-28 10-34-03.png
You do not have the required permissions to view the files attached to this post.
tailkinker
Newbie
Posts: 32
Joined: Tue Sep 17, 2024 11:06 am

Re: EOF .line csv mischief

Post by tailkinker »

Screenshot at 2025-02-28 10-34-10.png
You do not have the required permissions to view the files attached to this post.
BruceSteers
Legend
Posts: 2081
Joined: Thu Jul 23, 2020 5:20 pm
Location: Isle of Wight

Re: EOF .line csv mischief

Post by BruceSteers »

An array must be initialized before you can add to it.

Either use like this..
' initialize a 4 item string array
 Dim oldsend As New String[4]
 oldsend[0] = "first item"
 oldsend[1] = "second item"
 oldsend[2] = "third item"
 oldsend[3] = "fourth item"


Or use Add()
' initialize an empty string array
Dim oldsend As New String[]  ' Use "New" to initialize the array
oldsend.Add("first item")
oldsend.Add("second item")


For arrays a quick way to initialize can be just to use = []
Dim oldsend As String[]  ' non initialized array

oldsend = []  ' initialize as empty
oldsend.Add("first item")
oldsend.Add("second item")



PS.
it is not easy to read your code and impossible to copy text from those screenshot images.

please just copy and paste the text into "gb" code blocks instead of making multiple screenshots.

you can select the text and hit the "gb" button to make it show as gambas code.
(see attached picture)
You do not have the required permissions to view the files attached to this post.
tailkinker
Newbie
Posts: 32
Joined: Tue Sep 17, 2024 11:06 am

Re: EOF .line csv mischief

Post by tailkinker »

Bruce,

Initialize arrays - I should have realized that. I don't know why I forget the simplest things in Gambas that are second nature in VB.

I will copy the code and paste in the future. Didn't realize I could do that.

Lucille
tailkinker
Newbie
Posts: 32
Joined: Tue Sep 17, 2024 11:06 am

Re: EOF .line csv mischief

Post by tailkinker »

Bruce,

I finally settled on using read to count the number of lines in the csv file without adding them into a collection, then do my sorting, searching, edits, and so on. The main nutrient files I am working with have almost 100 columns for each line, and then hundreds of foods and ingredients. Even if I break that down to 100 main ingredients, it is still a lot to work with.

Still exploring though, and one of these days I will post what I finally come up with.

Lucille
User avatar
Quincunxian
Regular
Posts: 196
Joined: Sun Jun 25, 2017 12:14 am
Location: Western Australia
Contact:

Re: EOF .line csv mischief

Post by Quincunxian »

I'd go the object oriented way. It may be old school now but it seems to fit the requirement.
In Lucille's case, the parent object would be a Food(?) which is just a holder object for all other classes of Food.
I had thought to use 'Recipe' for this but figured that a recipe is a method of the food object - still not sure on this.
ie A Cake belongs to the food class and has some basic common properties like Flour, Milk & eggs.
There may be exceptions to these base properties but feel that it satisfies 98% of cakes that I know of.
The property values may change and other properties like Chocolate can be added to extend that class.
The parent class would also have properties/functions for cooking time and preparation methods.
I'd assume you would want an image assigned so that the user could see what the outcome was 'supposed' to look like. :lol:

Given how many ingredients there are, you would need to store these in a database with a simple interface to add, change and rarely to delete an item.
Drop and drag ingredients from a list into a new Food object where you would then specify the quantity.
The recipe item, which is the instructions on how to construct the food object would then have the step by step process of what ingredient to add in which sequence.
A Cooking object, which can be anything from refrigeration ( ice cream) , oven (cake) frying pan (pancakes) with specific times based on the appliance.
Gas fan forced ovens, wood fired ovens and electric ovens may have vastly different outcomes
I guess you would need utensils too. There are some specific tools you may need such as measuring cups - sure most cooks would have these but it is a good example.


It sounds like a fun project.
The coding is not really that hard. It's the initial design process that needs the thought.
Cheers - Quin.
I code therefore I am
User avatar
cogier
Site Admin
Posts: 1196
Joined: Wed Sep 21, 2016 2:22 pm
Location: Guernsey, Channel Islands

Re: EOF .line csv mischief

Post by cogier »

I am trying to find out how many lines are in my csv file.
All the above looks very complicated to me. I use CSV files a lot, but without using the CsvFile component. Try this:-

Dim sList As String[] = Split(File.Load(Application.Path &/ "test.csv"), gb.NewLine, "", True)

Print sList.Count
Post Reply