LoginLogin

What is an Array and how do I use them?

Root / Programming Questions / [.]

🔒
GrahamCreated:
So, I was following Mariominer's tutorial to add in the map data to my program (http://smilebasicsource.com/page?pid=40) , and the tutorial calls to use this line of code: DIM MAP[0] What follows next requires using an array, followed by a FOR loop to add in the background layers. I got the tutorial to work just fine, but I have no idea how or why this works. Can someone explain to me what an array is, and how to best use it? Also, keep in mind that I don't know much about coding, so please try to keep it in simple terms! Thanks so much, ya'll for all your help! \(^_^\)

an array is a value that goes into a variable. using DIM sets the dimensions for the array, so DIM ARRAY#[4] would make an array called ARRAY# and have 4 slots to access, from 0-3. then you could go ahead and access it by inserting a number between the brackets like LOL#=ARRAY#[2], which would grab the third value of the array. you can also set values in the array as you would any other variable, except adding the array slot to the variable name like ARRAY#[2]=7. edit: fixed a small error pointed out by the guy under me (SquareFingers) edit again: well hes not under me anymore since he deleted his post (well i think hes a he. i could be wrong)

Graham, in that example, DIM MAP[0] is creating an array with no elements. This is because the LOAD function is automatically resizing it as needed for the map, but this is a special case. Generally you will want to create arrays when you want to store several values of the same type (a bunch of numbers, or a bunch of strings). Arrays are handy because they are contiguous, meaning each element follows one after the other. So you can access them using an index. This fits nicely with things like loops that use incremental numbers. Not to mention, they are good for organizing large sets of data. Consider the following:
DIM TMP[5]
TMP[0]="A"
TMP[1]="B"
TMP[2]="C"
TMP[3]="D"
TMP[4]="E"

VAR M = 0

FOR M=0 TO LEN(TMP)-1
  PRINT TMP[M]
NEXT

PUSH TMP, "F"
PRINT TMP[5]
The above code creates an array with five elements, and then assigns each of them a string value. The for loop is able to access each element by the numerical index 'M'. When M is 0, it prints "A", when it's 1 it prints "B", and so on. Then at the end, the PUSH statement adds a 6th element "F", and prints it. This is a just simple example of how to use an array, but highlights some of the strengths. It's often handy to be able to look up a value based on a numerical index as opposed to a variable name. Like if you had 100 enemies in your game, would you want to do this: "VAR ENEMY1,ENEMY2,ENEMY3,..." for all 100 of them? It's much easier to do this: "DIM ENEMIES[100]" The second is much easier to define, is organized under one label, you can perform operations on every element quickly with a FOR loop, and you can add or remove items as needed. Hope that helps.

Wow guys, thank ya'll for so much help! All these tips are so helpful for someone without much coding experience! \(^_^\)

edit again: well hes not under me anymore since he deleted his post
Sorry about that, at first I just noticed that you fixed what I mentioned. So, it seemed there was no point in keeping that post. Only after I deleted it did I notice that its absence made another part of your post look wrong. Apologies!

Apologies!
no big deal lol its alright