LoginLogin

How Do You Use Arrays?

Root / Programming Questions / [.]

AlicekittyCreated:
I heard arrays are really useful in programming languages but I don't know how to use them. I want to know some good uses for them too and example code would be appreciated! :D Thanks! This part doesn't have to do with programming but I didn't want to make a whole new thread haha Do any of you listen to Kpop? I feel like I'm the only one lol! This is my favorite video! :-) https://www.youtube.com/watch?v=Rwr3j7N5JsA

Arrays are like mini-files in your program which can be saved and loaded even after you end the program. Let me try to write an example from memory:
DIM TEST[3] 'Creates a list for 3 variables.
 
TEST[0]=1 'The list starts at 0. This is the first item on our list.
TEST [1]=10 'Second item.
TEST[2]=10 'The third item on the list. Now let's put it to use.

IF CHKFILE("DAT:TEST")==TRUE THEN LOAD "DAT:TEST",TEST

'This checks for the data file named "test." The first part, DAT, is a type of file.
'The second part is the name of the file. Doesn't matter what it is as long as it's what you saved it as.
'The third part is where this data is applied, I think, since our list is labelled as TEST.
'Now let's use the arrays for something.

BEEP TEST[0] 'TEST[0] is 1, so it will play the 1st BEEP sound.
LOCATE TEST[1],TEST[2]:?"Hello, world!" 'This will print the phrase on beginning at X 10 and Y 10.
Hope this suffices. You can also use DIM (name)$[#] which can be used to store strings, so you can write down dialogue and use the coding above to print it. *By the way, to SAVE arrays, use SAVE "filetype:name",arrayname. Files that will hold, say, the player's name or string arrays, should be saved as TXT instead of DAT.

thank you!

thank you!
No problem. Tell me if there's an issue with the code and I'll try to solve it. Also, here's a cool thing you can do to easily see the contents of each array (also from memory):
'make sure you've already defined an array list with DIM or else the values will be unrecognized; same applies with saving and loading files.

FOR I=0 TO 10 '"FOR' is used to repeat something starting from the first value TO the second value. "I" is the first value, and we make it hold the number 0 and will increase to 10.

LOCATE 10,I:?ARRAY[I] 'Prints anything in our array beginning from 0 to 10 because FOR increases after each print.

NEXT 'allows FOR to end and continue on with the program.

Thanks for the advice guys I appreciate it :D I didn't know you could use them in other programs that sounds awesome!!! :D

@ResetReload Interesting... Can you tell me how to save the file so I can load it later on? I always save strings to text files and I felt like I was restricted to saving only one string to load later on but it seems like arrays allow you to store more? I'm sorry that I'm a little confused, I'm not that good at programming yet, I'm still learning haha :-) Thanks! Edit: Saw how on the same post, I'm an idiot...

@ResetReload Interesting... Can you tell me how to save the file so I can load it later on? I'm still learning haha :-) Thanks! Edit: Saw how on the same post, I'm an idiot...
Hey, it's no problem. :) We're all still learning here.

I should probably mention that when using FOR and arrays, make sure the second value isn't higher than the max amount of values in the array. example:
DIM Q$[2]
Q$[0]="hello"
Q$[1]="world"

FOR I=0 TO 4
?Q$[I]
NEXT
'This won't work because our list doesn't have 5 values, only 2.

Thank you I actually made that exact error (With a different value than Q$ of course haha) is there a way to save string arrays to text files?

Just save it as a DAT file.

Thank you I actually made that exact error (With a different value than Q$ of course haha) is there a way to save string arrays to text files?
Sorry for the late response. It's been a while since I used them so I had forgotten how they work. You need to make a non-dim value, which is just a letter or word followed by $, then save it as SAVE "TXT:FILENAME",array$, or something. I just made this program which explains it better than I could even though I wrote it like 5 minutes ago. I have horrible memory. N32PSXGD. Try messing around with that.

Thank you I actually made that exact error (With a different value than Q$ of course haha) is there a way to save string arrays to text files?
Sorry for the late response. It's been a while since I used them so I had forgotten how they work. You need to make a non-dim value, which is just a letter or word followed by $, then save it as SAVE "TXT:FILENAME",array$, or something. I just made this program which explains it better than I could even though I wrote it like 5 minutes ago. I have horrible memory. N32PSXGD. Try messing around with that.
i'll see what i can do.

You have any little programs/demos i could use for an accurate BG to SP collision? EDIT: Been struggling with an accurate background to sprite collision detection.

You have any little programs/demos i could use for an accurate BG to SP collision?
I don't know how to do BG collision yet, but I've been working on a simple SP collision thing. I'll have to "tutorial-ise" it, though, which may take a while. Do you want to look over it now or wait?

sure now would'nt hurt. thanks man!

sure now would'nt hurt. thanks man!
7E7NA3LJ should be it. This is just some random things I've been testing, so it's probably not too useful. I'll write the explainations tomorrow - I need to sleep.

sure now would'nt hurt. thanks man!
7E7NA3LJ should be it. This is just some random things I've been testing, so it's probably not too useful. I'll write the explainations tomorrow - I need to sleep.
alright. thanks dude! dont let the bed bugs bite. ;)

Awesome I actually need help with sprites too, I'm not too good with them haha :D

Awesome I actually need help with sprites too, I'm not too good with them haha :D
I'll get to work on an explanation tutorial thing.

sure now would'nt hurt. thanks man!
By the way, I made that red winged/horned demon sprite, so don't use it in a program you plan on uploading somewhere.