Gambas/Array
Back to Gambas
A simple array
editAn array is a list, filled with variables of the same type.
- You can fill an array with all the weekdays of a week.
- You can fill an array with the name of all the months of a year.
- You can fill it with the names of a class in your school.
- You can fill it with the numbers 0-9 and so on.
Arrays will shorten the length of your code, because all the members of the array can be reached with one loop. Every element of an array can be reached by its position in the array. Be careful: The first variable has the position zero.
Example:
The following list of names shall be stored in an array
Jack, John , Anne , Alice
You can use the array command for that. The output is done with a for-each loop.
The program: You need a form and a commandbutton to get it going.
ar AS String[] ' a string array is defined PUBLIC SUB Form_Open() ar = Array("Jack","John","Anne","Alice") ' the array is filled END PUBLIC SUB Button1_Click() element AS String FOR EACH element IN ar PRINT element NEXT ' the array is printed into the terminal window END
The Array command
editThe Array command is used with the following syntax:
array = Array ( Expression , ... )
It creates an array and returns it. The type of the array is the type of the first expression. The other expressions are automatically converted. You can use the square bracket syntax as an alternative to the Array() subroutine.
Examples
PRINT Object.Type(Array(2.4, 3, 3.2))
> Float[]
PRINT Object.Type(Array("2.4", 3, 3.2))
> String[]
PRINT [ "A", "B", "C" ].Join("/")
> A/B/C
The Addy program
editIn the following example an array is filled with integers. Then the elements are looked up in a for-each loop and added together. This miniprogram calculates the sum of a row of integers. You need 1 textarea, 1 textbox and 1 commandbutton to get the program going. You can also add negative integers. Very helpful is the Splitcommand and the Stringarray String[]
The Code:
PUBLIC SUB Button1_Click() text AS String summe AS Float elt AS String[] Sb AS String text = textarea1.Text elt = Split(text,Chr(10)) FOR EACH Sb IN elt summe = summe + Val(sb) NEXT textbox1.Text = summe END
When you start the programm, you can fill any number into the textarea. When you want to add another number just push the RETURN button.
The Split command
editArray = Split ( String [ , Separators , Escape ] )
This command splits a string into substrings delimited by Separators . Escape characters can be specified: any separator characters enclosed between two escape characters are ignored in the splitting process.
Note that Split takes only three arguments: if you want to use several separators, you should pass them as the second parameter, concatenated in a single string. By default, the comma character is the separator, and there are no escape characters. This function returns a string array filled with each detected substring.
Example: you need a command button on your form to get it going.
PUBLIC SUB Button1_Click() DIM Elt AS String[] DIM Sb AS String Elt = Split("Gambas Almost Means BASIC ! 'agree ?'", " ", "'") FOR EACH Sb IN Elt PRINT Sb NEXT End
Output in the terminal window:
Gambas Almost Means BASIC ! agree ?