Author Topic: strange on "n++"  (Read 2556 times)

0 Members and 2 Guests are viewing this topic.

pber

  • Guest
strange on "n++"
« on: June 19, 2014, 05:30:58 AM »
Hi Charles,

Code: [Select]
int n
dim as string tokens(128)

sub store(string token)
    tokens(n)= token
    n++
End sub

n++ '<<<<<<<< comment this and see the strange behavior

print n
store("xxx")

print n
store("xxx")

print n

...i missed something.

Charles Pegge

  • Guest
Re: strange on "n++"
« Reply #1 on: June 19, 2014, 05:41:36 AM »
Hi Paolo,

Indexbase 1 is the default, - so trying to access element 0, of the tokens array causes trouble.

You can work with indexbase 0, if you prefer, or any other indexbase for arrays. It can be changed at any time and may be confined to local scope, (inside a function, for example)

indexbase 0

Peter

  • Guest
Re: strange on "n++"
« Reply #2 on: June 19, 2014, 05:52:11 AM »
Hi Charles,

you see, indexbase 1 makes lot of trouble for people who are new here.
just change it to 0, what really is standard.

we need no settings  like thinbasic here!   

Charles Pegge

  • Guest
Re: strange on "n++"
« Reply #3 on: June 19, 2014, 05:59:49 AM »
Hi Peter,

The reason why I chose 1 is consistency with character indexing in strings, traditionally 1 to len(s). Iterations are also a bit cleaner: for i=1 to n : v[ i ]= ...

Sophisticated users and developers may opt for indexbase 0, or even indexbase -1 :)

pber

  • Guest
Re: strange on "n++"
« Reply #4 on: June 19, 2014, 06:06:48 AM »
sorry... i could expect some array-related issue.

thanks to all