Page 1 of 1

StringField()

Posted: Fri Dec 15, 2006 8:25 am
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

Re: StringField()

Posted: Fri Dec 15, 2006 8:53 am
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 !

Posted: Fri Dec 15, 2006 10:13 am
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

Posted: Fri Dec 15, 2006 10:25 am
by netmaestro
All the string stuff that involves counting is 1-based, Mid, FindString, StringField, etc.