csv files, ListBox and loop/Split|Join

In a current project, I have to read a text file (in fact, many) that hold data as csv container files.

As an example, I use the following code to fill the ListBox Heading:

First line of the csv file:

"Cover","Archive name","Size","Date added","Download"

Xojo code:

[code] // Fill the ListBox Heading
For HeadIdx = 1 To HeadCnt
// Get the correct value for this heading
HeadField = NthField(LibgenURL,",",HeadIdx)
TheField = Mid(HeadField,2,Len(HeadField)-2) // remove the two " (*)

  // Fill the heading
  LB.Heading(0) = TheField
  
  // To avoid 1, Infinite Loop
  If UserCancelled Then Exit
Next

[/code]
In reading the documentation, I happen to read:

NOTE: Using NthField in a loop to extract fields from a string is inefficient. You should use Split for this purpose.

As a nice young boy, I wanted to follow the documentation advice. What a crazy idea did I had ! In line noted (*), you can understand how I remove the leading and ending quotes for each field. Using Split and Join seems to me a bad idea in this respect because I have to remove them in some way.

My new code looks like:

HeadStr() = URL.Split(",") LB.Heading(-1) = Join(HeadStr,chr(9))

The new new code have two lines vs the previous code (6 lines), or just a one liner:

   LB.Heading(-1) = Join(URL.Split(","),chr(9))

But that code do not remove the leading and ending quotes from the text stored in a csv file.

So, excepted if we have a solution, I will come back to my ineficient code (that works nice for hundred lines).

replace ‘st’ with your string name

LB.Heading(-1) = NthField(st, EndOfLine, 1).ReplaceAll(",",chr(9)).ReplaceAll("""","")

Well, you should be splitting on “,” not just the comma for csv files and then strip off any quotes in the first and last fields. All that string manipulation can be a bit hairy (and slow). I really prefer tab delimited files because (I feel) they’re a bit easier to split.

Or, better yet, just a JSON string and that way you can use them as dictionaries and they reat/write to a text file nicely.

But, in the long do what makes sense to you. As long as you’re not doing thousands of NthField calls you’ll probably be okay. Yes, using split is faster but if your code is already done (and working) it might not make much sense to rewrite. Premature optimization can really put a damper on coding. Get it working. Test. Optimize the parts that need it.

amen!! and some of us fall into that trap too often.

I started to use “,” as field delimiter. The problem with that is with the first and last " in each lines stays.
Tab delimited files does the same (if I use surrounding quotes).

Embedding fields with surrounding quotes have the adventage of keeping everything inside each ‘fields’. Say, you have a comma or a quote inside your field and - if your field is not quote (or anything else) embedded, you’re in trouble: your software is fooled.

So, I will come back to the loop code I used before (that I do not deleted, but keep in comments…).

Thank you for your help.

Only if you use a more sophisticated method of parsing than Nthfield or Split. Neither will handle an embedded comma correctly.

What I am doing is to replace the comma by a quote single base &u201A “‚” in the field data before saving. To the user it looks like a comma, but it does not mess up the separators.

That said, whenever possible, I rather use Tab delimited. It is way more robust.

So you need to escape the Tabs (if any) in the text data.

Thank you all for the answers.

Norman has a csvparser on his web site

Jim

Thanks Jim, I will get an eye on it.

[quote=177396:@Jim Shaffer]Norman has a csvparser on his web site

Jim[/quote]
I was unable to open the .rb file with Xojo to get an eye on it Xojo freezed).

FWIW, the SimpleCSVSample.rb file opens and runs on 2015R1 on Mac.

That (SimpleCSVSample.rb) was what I meant using .rb.

No, Xojo 2015r1 (current) on OS X Yosemite 10.10.3 PB (the last one) does not open it: it freeze Xojo.

Note that I only checked once. I may check it a second time if you are sure that Xojo 2015r1 (and not a newer) loads “SimpleCSVSample.rb”.

Thanks for the info.