LoginLogin
Might make SBS readonly: thread

How Do Arrays Work?

Root / FAQs / [.]

📌
MZ952Created:
I've been coding with SmileBASIC for several years on Petit Computer, and am now transitioning to the new Petit 3. I am seeing the use of arrays in other programs, and they seem to be incredibly useful, but i have neglected to learn anything of them in the years i've used Petit Computer. So... what exactly are they? I've experimented with them (sorta) and read about them, but their purposes still escape me. I understand that they contain a string of some sort, and i believe the string is as long as the number set inside the [ ], but i have no clue as to how to use one. DIM A[3] What can i do with it, and how do they work?

An array can contain various data types, I'm not sure for BASIC but I know they can either string (DIM A$[3]) or integer (DIM A[3]). They should also be able to handle any other type; such as boolean. I'm sure you understand this much from your reading but I'll say it anyway. An array is a set of values stored in one variable, however we can manipulate these values individually. We can remove values from the index or add, and call upon them. Keep in mind an array's index starts at zero, so the maximum value will always be array size-1. They can be useful for a variety of task, and they can be avoided but you will be doing much more work. Imagine you have a list of names to be used in a database, rather than having individual string variables for each name, you can store them all in one array. Again I'm not sure how BASIC handles array, they are likely fixed size collections. However if I'm wrong that is another strength of arrays, they are flexible and will increase in size as new elements are added.

That, to me, seems to be the same thing as utilizing string commands. Like, storing multiple names in a single string, you could use this
A$="Name2"
B$="Name1"
C$="."
TXT$=A$
TL=LEN(TXT$)
TXT$=SUBST$(TXT$,TL,0,C$)
TL=LEN(TXT$)
TXT$=SUBST$(TXT$,TL,0,B$)
TXT$ should display "Name2.Name1", which you can store in a single string, then if you wanted to retrieve one of the names, you could use this bit of code,
P=INSTR(TXT$,".")
TL=LEN(TXT$)
D=TL-P
B$=MID$(TXT$,P,D)
B$ should output "Name1" Is there a difference from this and arrays?

An array is like a set of values that can be manipulated. For example: DIM S$[20] creates a string array of 20 entries. This one array can store 20 different strings, each at one of its entries.
S$[0]="HELLO"
S$[1]="WORLD"
PRINT S$[0]+S$[1]
This would print HELLOWORLD. An array can be any of the three types: integer (%), real (#) or string($). This allows you to manage a set of data as one unit.

I know they can either string (DIM A$[3]) or integer (DIM A[3]). They should also be able to handle any other type; such as boolean.
Variables without a suffix, even arrays without a suffix, are numerical - but what type of numerical, depends on whether your program has OPTION DEFINT or not. Without, no suffix means floating-point. With, no suffix means integer. When you want to be clear that integers are intended, it's best to use the % suffix, and when you want to be clear that floating-point is intended, it's best to use the # suffix. There is no boolean type in SmileBasic. As for MZ592's manipulation of strings: it requires that "." not be part of any of the substrings, and identifies the substrings by their beginning and end, rather than by a more convenient sequential indexing.

I know they can either string (DIM A$[3]) or integer (DIM A[3]). They should also be able to handle any other type; such as boolean.
Variables without a suffix, even arrays without a suffix, are numerical - but what type of numerical, depends on whether your program has OPTION DEFINT or not. Without, no suffix means floating-point. With, no suffix means integer. When you want to be clear that integers are intended, it's best to use the % suffix, and when you want to be clear that floating-point is ontended, it's best to use the # suffix. There is no boolean type in SmileBasic. As for MZ592's manipulation of strings: it requires that "." not be part of any of the substrings, and identifies the substrings by their beginning and end, rather than by a more convenient sequential indexing.
Ah okay, thanks for the clarification!

Thanks everyone for helping me understanding these things!

I think the best way to explain it is this is you could have var0=1 var1=89 var2=67 And then print all those things like print var0 print var1 print var2 And that would be very painful to do if you went up to var33 or something, which I have done before before I knew how arrays worked. so using arrays you can go Dim Var[3] Var[0]=1 Var[1]=22 Var[2]=838 (better to do this some other way) for u=0 to 2 Print var next