Posted: 11th Sep 2003 14:28
about arrays (old hands please note I am ignoring the zero element for simplicity)
dim numbers(9)
makes a list of 9 numbers, so you could do
numbers(1)=10
numbers(2)=20
numbers(3)=30
......
numbers(9)=90
you can use them just like normal numbers
numbers(9)+numbers(1)=100
but you need to provide that number in the brackets so that you know what number in the set "numbers" you are talking about, so the variable numbers can hold 9 values
numbers
(1)(2)(3)(4)(5)(6)(7)(8)(9)
the real use of arrays is that you can get the computer to change the number in the brackets so that it can get to them on it`s own, for example the following code will load 10xi into each of 9 numbers
dim numbers(9)
for i=1 to 9
numbers(i)=i*10
next i
or this code will read a chunk of data into the numbers
dim numbers(9)
for i=1 to 9
read value
numbers(i)=value
next i
data 1265,234,9087,3434,314,415,456,765,384
you can use them to hold the status of an army of 2000 attacking machines
for i=1 to 2000
if robot_power(i)=0 then gosub explode_the_sucker
next i
lets you check the status of 2000 robots in a few lines, and to hurt robot 123 that you have in your sites?
robot_power(123)=robot_power(123)-20
just like you would a normal variable, just remeber you need to supply the correct values, you could modify the above code to keep track of 2000 spheres and delete them when their power reaches zero or whatever else your code requires, then you can also use them to indicate what happens on (for example) a matrix, since they can use two numbers for the index like this
dim grid(4,4)
gives a set of numbers lke this
grid
(1)(2)(3)(4)
(5)(6)(7)(8)
(9)(10......etc
(13......16)
so grid(2,2) refers to the number at the position in the array 2 across 2 down (6) or grid(1,4)=13, you could use a bigger grid to store the positions of mines hidden under a matrix and explode one when the player is that the correct tile position, you check that position in the matrix and if the player is on a mine then BOOM!
you can make them 3D too
dim cubeworld(10,10,10)
4D
dim hyperworld(10,10,10,10)
7D
dim headexploder(10,10,10,10,10,10,10)
or as many dimensions as you like, you can also make arrays from strings, types and floats...eg
dim word$(200)
dim decimal#(50)
dim types(20)
heres an example that remembers 10 individual lines and then lets you recall a specific line
dim messages$(10)
for i=1 to 10
input a$
messages$(i)=a$
cls
next i
do
repeat
print "what number message do you wish to recall?"
input index
until (index>0 and index<10)
print messages$(index)
print "press space to continue"
wait 200
repeat
until spacekey()
cls
loop
you can use arrays to record movements or store the scripts for actions, the position of objects in 3D (or 23D) space and loads of other useful stuff, they also remove the load when you want a lot of repetitive numbers handling that are all the same type, heres a pack of cards
dim cards(52)
for i=1 to 52
cards(i)=i
next i
but they are all in order, lets shuffle them up 300 times by swapping any two randomly chosen cards around
for i=1 to 300
first_card=rnd(51)+1
second_card=rnd(51)+1
hold_first_card=cards(first_card):` this saves the 1st card value
card(first_card)=card(second_card):` this puts the 2nd card in 1st cards slot
card(second_card)=hold_first_card:` and this puts the 1st cards value in the 2nd cards slot
next i
that swaps two different randomly chosen cards around 300 times, that shuffles the pack thoroughly, to see the result and verify that the shuffle occured with no duplication of cards just do
for i=1 to 52
print cards(i)
next i
hope that explains arrays better, they are fast since they are held in memory and they simplify your code, you realy need arrays to do anything cool, unless you plan to write code like
robot1=23
robot2=45
robot3=32
.
.
.
.
robot1000=3
instead of
dim robot(1000)
for i=1 to 1000
read robot_type
robot(i)=robot_type
next i
data 23,45,32........
......
data .....,3
cheers.
Mentor.