forums

TNT Basic Forums > Programming Q&A
Giving values to arrays
< Last Thread     Next Thread >
Author
Thread        Post A Reply

03-24-2003 20:24

Posted by:
matteo

Location:
Venice, ITALY!

Click Here to Email matteo   Find more posts by matteo

So, I have my array, lets call him "ArrayWithLongName[100]"

Now, if I want to give him values one for one, I should do:

ArrayWithLongName[0]=3
ArrayWithLongName[1]=2
ArrayWithLongName[2]=4
...
ArrayWithLongName[99]=1

Now, somebody knows a faster way? say writing the numbers somewhere in the code and then making TNT read them? I know you can do this with "data" and "read", but you still would have to write:

data 3,2...

read ArrayWithLongName[0],ArrayWithLongName[1]...

Can I avoid this?

03-24-2003 22:24

Posted by:
Machi

Click Here to Email Machi   Find more posts by Machi

Hi,

This is one of the few times that you can do something useful with the TNT file handling. Create a text file with the values and create a very small loop to increment the array index and read and move the values into the array.

Mark

03-30-2003 20:29

Posted by:
Mark Tully

Location:
TNT HQ, England

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

Reading values into an array

You can use data and read statements like so:

int index,count,MyArray[]

restore MyArrayValues ' move the read position to label

read count ' number of values
dim MyArray[count] ' create array of correct size
for index=0 to count
read MyArray[index]
next index

... other code here if you want ....

MyArrayValues:
data 6 ' array of six elements
data 2,3,56,4,233,56 ' six values

This example creates the array to be the right size then reads all the values into it. It determines where to start reading the array contents from by use of a label, in this case called 'MyArrayValues'. Note that the dim command will destory and redimension an array even if it already exists.

Hope this helps,

Mark

03-30-2003 21:11

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Wow, I didn't know there was a way to put the read point at a label! That makes things easier for me. Guess I should read the commands over again...

03-31-2003 07:16

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,
Probably just missing something obvious here, but what is the difference between dim and int in holding values?

03-31-2003 15:34

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

in qbasic at least, dim is used for dimensioning an array, int would be for specifying the data type of any variable, including arrays.

03-31-2003 21:05

Posted by:
Mark Tully

Location:
TNT HQ, England

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

Dimensioning an array

You can dimension an array in two ways:

1)

int myarray[] ' declared as array but size not yet specified
...
...
dim myarray[40] ' specify a size for array you can now use it


2)

int myarray[40] ' declare array and specify a size. You can use it immediately


In both cases you can redimension an array to another size with another dim statement.


dim myarray[30,2] ' make it into a 2D array of size 30*2

The docs on restore are lacking I'm afraid, we'll update them for the next release.

Hope this helps,

Mark

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.