forums

TNT Basic Forums > Programming Q&A
Array indexes
< Last Thread     Next Thread >
Author
Thread        Post A Reply

03-24-2003 11:10

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,

For part of my code I want to see the contents of the string array that is been processed on screen, however, when I do I get the following runtime error for the last entry in the array elements. Everything else works OK. This also happens when I tried reming the last entry and changing the array amount.
Error : Array index out of bounds.

The code is included below.

string MyArray[21]
MyArray[0]="Blah, blah"
MyArray[1]="Blah, blah"
'Few more of these . . . .

MyArray[21]="Blah, blah"

int x=0

repeat
x=x+1
string n=right str(MyArray[0],x)
print n
string n=right str(MyArray[0],x)+MyArray[x]
print n
until x=21

thanks,
Mark_667

03-24-2003 16:08

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

option base 1

most of the time the indices of an array start at 0, so if your trying to search the array starting at 1 the last element in your search will be too large. in QBasic you can use the command 'option base [int]' to start at whatever number you want. i don't know if there is a tnt basic equivalent, but if not just start counting at 0.

03-28-2003 10:50

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,

Tried taking it out of a loop to just do the first item, but still got the same error:

int x=0
string n=right str(MyArray[0],x)
print n

Here zero is my starting point as x has not been incremented,and the last item in the search is never even reached.

This also happens when I do the loop with just one pass programmed in:
int x=0

repeat
string n=right str(MyArray[0],x) 'Test first one without incrementing x then do others normally.
x=x+1
string n=right str(MyArray[0],x)
print n
string n=right str(MyArray[0],x)+MyArray[x]
print n
until x=1

Also what exactly does the 'option base [int]'function do? I don't think there's a TNT equivalent.

Thanks,
Mark_667

03-28-2003 11:47

Posted by:
Machi

Click Here to Email Machi   Find more posts by Machi

Hi - Puffin was quite right

Your initial problem, but not the only one, is where you declare the array to have 21 elements

string MyArray[21]

and then proceed to declare the contents of the 22nd element

MyArray[21]="Blah, blah"

the last addressable element in the array is actually

MyArray[20]="Blah, blah"

because the first index is 0

So the rest of your code won't be executed past this initial error

Another suggestion regarding your general approach would be to use "for" loops instead of the repeat loops

ie something like

for x = 1 to 20
print right str(MyArray[0],x)
print right str(MyArray[0],x)+MyArray[x]
next x

would replace the loop in your code

Hope this helps

03-31-2003 07:14

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,
The for loop worked fine (why didn't I thnk of it?, but is there a way of printing the entire string instead of just segments of it as in my example? It is OK for shorter entrys, but gets cut off for some of the larger ones, I tried:

print left str(MyArray[0],x), mid str (MyArray[0],x), right str(MyArray[0],x)

to try to get all of it on one line, but this comes up with an irritating error about comma separators. Is there a command such as:

print entire string(MyArray[0],x)

or plans to implement one like it in the future?

03-31-2003 14:30

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

When using the print command, you can print multiple elements on the same line if you separate them by semicolons ( ; )


string a="Ah, "
string b="hahaha"
string c="ooooo"
print a;b;c

'output: Ah, hahahaooooo

'or

print a;"I see..."
'output: Ah, I see...

yeah, anyway...

03-31-2003 15:28

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

try somthing like this

str n = "david"
str a = "19"

print n + " " + "is" + a + " " + "years old"

' output: david is 19 years old

remember spaces art automaticaly put in, but you could also do this:

print n + " is" + a + " years old"

04-02-2003 10:01

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Thanks, tried what you suggested and it worked, any chance of you guys implementing a functional equivalant in TNT in the form of something like:

print entire string(MyArray[0],x)

04-03-2003 19:57

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

That wouldn't be hard to add, we should probably do some sort of array printing command.

Cheers,

Mark

04-04-2003 09:53

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Irritating development

Hi,
Tried eekaydee’s recommendation, and from what I know, the below example should work fine, but the third line of code, see below, is highlighted as expecting a comma after the first 'x'. The others work as expected, but the line with the mid string variable doesn't - could this be a bug in this particular function, or just another example of my bad coding?

for x = 0 to 21

string a =left str(DeletionArray[0],x)+(DeletionArray[x])
string b =mid str (DeletionArray[0],x)+(DeletionArray[x])
string c =right str (DeletionArray[0],x)+(DeletionArray[x])

print a+b+c

next x

thanks,
mark_667

04-04-2003 23:22

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

for x = 0 to 21

string a =left str(DeletionArray[0],x)+(DeletionArray[x])
string b =mid str (DeletionArray[0],x)+(DeletionArray[x])
string c =right str (DeletionArray[0],x)+(DeletionArray[x])

print a+b+c

next x


What on earth is this code supposed to do? ( don't take it badly :) )

it means:

first loop:
take the 0 left character of the first array, and then add the current array
take the 0 mid character of the first array, and then add the current array
take the 0 right character of the first array, and then add the current array
print it all

second loop:
take the 1 left character of the first array, and then add the current array
take the 1 mid character of the first array, and then add the current array
take the 1 right character of the first array, and then add the current array
print it all

third loop:
take the 2 left characters of the first array, and then add the current array
take the 2 mid characters of the first array, and then add the current array
take the 2 right characters of the first array, and then add the current array
print it all

Well I think you got the picture!

If you only want to print an array, I suggest:

for x=0 to 21
print DeletionArray[x]
next x

04-07-2003 08:30

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

What on earth is this code supposed to do? ( don't take it badly :) )

Made it as part of a hoax virus for April fool's day, couldn't quite de-bug it in time, though! :)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.