This assumes you already know what a variable is...
This should get you started with arrays.
What is an array? How do I make one?
An array can be thought of as a dresser with boxes inside:
The first number you specify is how many dressers there are.
The second number is how many cabinets are in each dresser.
The third number is how many drawers are inside each cabinet.
The fourth number is how many boxes are inside each drawer.
The fifth number is how many mini-compartments each box has.
You can only specify up to 5 different numbers or sections. However, you do not need to specify that many.
In fact, you could just specify 1 if you wanted to. If you wrote
dim Array(1)
it would create an array with one dresser. The DIM command is used for creating arrays. To erase an array, use UNDIM. You can call an array anything you want. I want to call it "hat" now. If you wrote
dim Hat(1,2)
it would create an array named HAT that has 1 dresser and that dresser would have 2 cabinets.
dim Hat(5,3)
it would create an array named Hat that has 5 dressers, each one having 3 cabinets.
dim Hat(5,3,4)
That makes an array named Hat that has 5 dressers, 3 cabinets in each dresser, and 4 drawers in each cabinet. We could keep on doing this, until we got to boxes with sections inside. I'm going to keep moving on though.
How to put stuff inside arrays
Here is where arrays come in useful: You can put stuff inside them! Here's an example:
Say you had an array named Hat again. Hat has 2 dressers and 5 cabinets in each dresser. And say you wanted to put the number 5 inside the third cabinet on the second dresser.
Well, you'd create the dressers and cabinets like this:
dim Hat(2,5)
and then you'd put that number five into it like this:
Hat(2,3)=5
It's that simple! notice how the first number says it's the second dresser and the second number says that it is the third cabinet. You can just as easily put variables inside arrays as well:
RaceCar=5
Hat(2,3)=RaceCar
You've put the variable called "RaceCar" (don't ask lol) inside the third cabinet in the second dresser.
You are not limited to only numbers though. Just like variables, arrays can take many different shapes or forms; three of which are INTEGERS, STRINGS and REAL numbers, also known as FLOATS. The one that we have been using can hold only INTEGER numbers. You could make an array that is meant to hold only REAL numbers. You could also make one that is meant to hold only STRINGS!
To make an array that holds REAL numbers (the ones with decimal points), write this:
dim Hat#(2,5)
Notice the "#" sign after "Hat". That's what makes it a real number holding array. Now how would you make one that holds strings? Take a minute to figure it out, then open the snippet below.
It's that "$" sign for strings! You would assign these arrays values (in other words, you would put things inside these arrays) in the same way as you would with our old array. For real numbers, write
Hat#(2,3)=5.4
With variables, it's
RaceCar#=5.4
Hat#(2,3)=5.4
The same goes for string arrays too.
Hat$(2,3)="This is a string cool!"
With variables, it's
RaceCar$="This is a string cool!"
Hat$(2,3)=RaceCar$
Alright, here's a final example that creates a STRING array with 5 dressers and 4 cabinets and prins what's in dresser 2 cabinet 3:
rem Create the array
Dim Hat$(5,4)
rem Put a string in Dresser 2 Cabinet 3
Hat$(2,3)="Hello there!"
rem Print it to the screen as well as telling the user to "Press a key to exit"
set cursor 0,0
print "Here's what's in dresser 2 cabinet 3: ",Hat$(2,3)
print "Press a key to exit"
rem Wait for the user to press a key, then exit.
suspend for key
And here's an example of how to use variables to do it:
rem Create the array
Dim Hat$(5,4)
rem Allow the user to type something in
input "Type something, I'll put it into an array: ",UserTyped$
rem Wait 0.2 seconds for you to take your finger off of the Enter Key.
wait 200
rem Put your string in Dresser 2 Cabinet 3
Hat$(2,3)=UserTyped$
rem clear the screen
cls
rem Print it to the screen as well as telling the user to "Press a key to exit"
set cursor 0,0
print "Here's what you put in dresser 2 cabinet 3: ",Hat$(2,3)
print "Press a key to exit"
rem Wait for the user to press a key, then exit.
suspend for key
That's all for now, I'm in a rush so I may add some more later. Anyway, tell me what you think so far it's my first tutorial.
EDIT: Alright, I've decided to add more.
Saving and Loading Arrays
Saving arrays and loading arrays is not very hard at all. How do you do it? You can use the "SAVE ARRAY" and "LOAD ARRAY" commands, that's how! However these 2 commands are not as simple as they look. So, to get around this, let's try to save our old pal "Hat". Remember Hat? The last time we saw him he had 5 dressers and 4 cabinets. To create him we had written:
Dim Hat(5,4)
Now to save him in a file. We can actually save Hat as whatever name we want to! In fact, he could be called: "Ping Pong Table". Secondly, we need to tell the command that we want to save Hat. Now the SAVE ARRAY command is one who isn't so bright. All he can know is how many dressers Hat has. If you tell him anything else he'll return some kind of annoying error. When you save arrays, they will appear as a file in its folder like everything else. You can go and browse your projects folder or something and find it there. So, in order to save Hat as "Ping Pong Table", we can write:
Save Array "Ping Pong Table",Hat(5)
Notice how I did not include the number of cabinets, just the number of dressers. The same goes for loading the array. IF you have already saved an array, you can load it again using the "LOAD ARRAY" command! To load Hat, just replace Save Array with Load Array! Both SAVE ARRAY and LOAD ARRAY are both not bright enough to handle more than the number of dressers that Hat has. Keep in mind that loading an array will overwrite what it has in it at the moment, and replace it with whatever you are loading. So, to load hat (you must have already saved Hat, of course) we would write:
Load Array "Ping Pong Table",Hat(5)
And that would load ALL of the contents of Hat into Hat again. You must have already created the array you are loading into (here it's Hat) using the DIM command though. You do not have to load into Hat though, even when you saved Hat! If you made another array called "PLANK" with the EXACT same number of dressers and the exact same number or cabinets in each dresser, you can actually load Hat into this new copy of Hat! For example, if you created "Plank" in the same way we did hat, like this:
Dim Plank(5,4)
We could load "Ping Pong Table" into Plank instead of Hat, like this:
Load Array "Ping Pong Table",Plank(5)
You can save and load String and Real number arrays in the same way! Say you made a string like this:
Dim Hat$(5,4)
You would save it like this:
Save Array "Ping Pong Table",Hat$(5)
All you need to do is add that "$" sign for STRING!
To load, do this:
Load Array "Ping Pong Table",Hat$(5)
And again, there's just that $ sign for STRING. The same goes for real numbers, but if you really need an example, here you go:
Dim Hat#(5,4)
There we create the array as a REAL number using that # sign.. and here is how we save it:
Save Array "Ping Pong Table",Hat#(5)
And here's how we load:
Load Array "Ping Pong Table",Hat#(5)
More Filename stuff
Did you know that you can use variables to save arrays as well!? Well, you can, and you can do it like this:
HatNeedsSaving$="Ping Pong Table"
Save Array HatNeedsSaving$,Hat(5)
You can also save it inside folders! You could save it in a folder called "Arrays" like this:
Save Array "Arrays/Ping Pong Table",Hat(5)
In order to save it in a folder called Arrays using variables, you could write:
HatNeedsSaving$="Ping Pong Table"
Save Array "Arrays/"+HatNeedsSaving$,Hat(5)
Or, you could write:
HatNeedsSaving$="Arrays/Ping Pong Table"
Save Array HatNeedsSaving$,Hat(5)
Both do the same thing. The filename is just like any other string. You can manipulate it using the STRING commands! This includes combining strings, like above! This would save it as "Table" by taking a part of the string "Ping Pong Table"!
HatNeedsSaving$="Ping Pong Table"
Save Array right$(HatNeedsSaving$,5),Hat(5)
And that's all I have to say (for now
)! To finish up, here are some examples:
A FULL EXAMPLE:
rem create the array that we will save
Dim Hat(5,4)
rem Create another array that is the same as Hat. We will load into this one.
Dim Plank(5,4)
rem Place a number inside the array
Hat(3,2)=42
rem Start the cursor at 0,0.
set cursor 0,0
rem Ask the user what they want to call the array
input "What do you want to call the array? ",ArrayName$
rem Clear the screen of text and tell the user that we have placed 42 inside Hat's 3rd dresser second cabinet.
cls
print "We have placed the number 42 inside Hat's third dresser second cabinet."
print "We are saving now."
rem Wait for the user to press a key and for the release of that key.
wait key
wait 200
rem Save the array as what you said
Save Array ArrayName$,Hat(5)
rem Get rid of hat because we don't need it anymore.
undim Hat(5,4)
rem Load what Hat used to have into the array called Plank
cls
print "Now, we load that 42 into Plank..."
rem Wait for the user to press a key and for the release of that key.
wait key
wait 200
Load Array ArrayName$,Plank(5)
rem Print the contents of Plank
cls
print "And here it is, what Plank now has in Dresser 3 cabinet 2: ",Plank(3,2)
rem wait for the user to press a key
wait key
rem Wait .2 seconds for the user to release the key.
wait 200
cls
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem ~~~~~~~~THE FOLLOWING IS UNIMPORTANT TO ARRAYS~~~~~~~~
rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rem ask the user if they want to delete the file because they are all done with the example.
input "We're all done. Do you want to delete the file?(y/n)",answer$
rem Translate their answer into lower-case letters.
answer$=Lower$(answer$)
rem Translate their answer again, this time making it into only the first letter
answer$=Left$(Answer$,1)
rem Wait .2 seconds for the user to release the enter key.
wait 200
rem Clear the screen of text
cls
rem If you said yes, then erase the fila and say it did.
rem IF you said no, then don't erase the fila and say it didn't.
if Answer$="y"
delete file ArrayName$
print "THE FILE IS GONE."
else
print "THE FILE IS STILL THERE THEN. IT HAS NOT BEEN ERASED."
endif
rem Wait for the user to press a key.
wait key
Here's a very simple example. It's meant more for looking at, not running:
rem Create the array called Hat.
dim Hat(5,4)
rem Put the number 20 in cabinet 3 on dresser 2.
Hat(2,3)=20
rem Save Hat as it is now, with that 20. It's saved as Ping Pong Table.
Save Array "Ping Pong Table", Hat(5)
rem Make Hat equal 99.
Hat(2,3)=99
rem Load our saved Hat, thus putting that 99 back to 20, because we saved it like that.
load array "Ping Pong Table",Hat(5)
rem Print the contents of Hat unto the screen.
print "Hat has been loaded. Dresser 2 cabinet 3 holds... ",Hat(2,3)
rem Wait for the user to press a key.
wait key
rem delete the file because we are done.
delete file "Ping Pong Table"
And if you wanted to, you could even make your own file extensions! You could make Hat into a .pingpong file if you wanted to. All you'd need to do is add a ".pingpong" to the end. To save that, you'd write:
Save Array "Ping Pong Table.pingpong",Hat(5)
and to load it, simply write:
Load Array "Ping Pong Table.pingpong",Hat(5)
There you are. Custom file extensions, to help you read arrays through your programs. This way, you could have one game called "The Big Car", and have it's array data saved as a "TBC" file, that way you could reuse the same name but just change the extension! Here's another way you might do it using variables:
First, you may specify the name of the file itself:
HatNeedsSaving$="Ping Pong Table"
Second, you might store the name of the extension in a variable.
HatExtension$=".pingpong"
Then, to save that you'd write this:
Save Array HatNeedsSaving$+HatExtension$,Hat(5)
So, in total that looks something like:
HatNeedsSaving$="Ping Pong Table"
HatExtension$=".pingpong"
Save Array HatNeedsSaving$+HatExtension$,Hat(5)
And that's all there is to that!