StringField()

Everything else that doesn't fall into one of the other PB categories.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

StringField()

Post by va!n »

Some hours ago i have started a small program and i thought there was a bug somewhere in PB, because i had everytime wrong results and tried to cut down my code and try to find the problem. I must say, when ever i code a loop or count something, i will start at 0 and not at 1...

Now, after an extreme smoking head and trying to cut down my source more and more, i found the problem and i cant understand the reason that StringField() will start at position 1 instead at position 0 :shock:

To understand and see what i mean... here is a small example i did, before i noticed in the docs, that it will start at position 1 :roll:

Code: Select all

; 
; *************************************************************************************
; *
; *  Just try to split the sTxtRead.s string into seperate parts while using StringField()
; *  and save the results to our aTxt.s array! I dont know why, but it seems the first 
; *  string part of out sTxtRead.s will be read two times by using StringField() instead
; *  just only once.
; *
; *  Array:        Should be:        PB result is:  
; *  -------------------------------------------------
; *  aTxt.s(0)     --> part1         --> part1
; *  aTxt.s(1)     --> part2         --> part1
; *  aTxt.s(2)     trying            --> part2
; *  aTxt.s(3)     to                trying
; *  aTxt.s(4)     show              to
; *  aTxt.s(5)     the               show
; *  aTxt.s(6)     bug               the
; *  aTxt.s(7)                       bug      
; *                                        
; *************************************************************************************

Dim aTxt.s(8)

sTxtRead.s = "--> part1;-->part2;trying;to;show;the;bug"
  
For i = 0 To 7
  aTxt(i) = StringField(sTxtRead.s, i, ";")
  Debug aTxt(i)
Next
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Re: StringField()

Post by gnozal »

va!n wrote:i cant understand the reason that StringField() will start at position 1 instead at position 0
Because it's the 1st string, the 2nd string, the 3rd string etc... and not an array index ?
Anyway, it works like it is written in the docs, so it's ok for me.
The worst thing would be to change this and break all existing code !
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Post by Kukulkan »

I agree with gnozal,

Example:

There are four apples in a row. You want to tell someone to give you the third apple. He would count 1 .. 2 .. 3 and give you the third apple. You would never begin to count the first apple by saying 0...

So, maybe, StringField() should raise an error with index < 1. But it is really ok for me, too.

Kukulkan
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

All the string stuff that involves counting is 1-based, Mid, FindString, StringField, etc.
BERESHEIT
Post Reply