This chapter describes the FOR-EACH control structure. It is another form of loop control structure.
Syntax for the FOR-EACH control structure:
FOR EACH Variable IN Expression <Instruction(s)> NEXT
In the project GBTT (GamBas-ToolTipps) all file paths of the files are stored in an array in which the search term was found. For control purposes, all saved file paths are then displayed in the console of the Gambas IDE for testing purposes:
... aSuchDateiArray.Sort(0) ' For control: FOR EACH sDateiPfad IN aSuchDateiArray PRINT aSuchDateiArray[iCount] NEXT
First, all output of the (console) command' df' is stored in the variable sResult. Then all lines of sResult are read from a temporary array (? split (sResult,“\n”). Finally, each line of the array with the scan command is divided into individual elements according to the predefined pattern and displayed as a separator with the pipe character that is freely selected here:
DIM sResult, sLine, sElement AS String SHELL "df" TO sResult FOR EACH sLine IN Split(sResult, "\n") FOR EACH sElement IN Scan(sLine, "* * * * *") PRINT sElement; "|"; NEXT ' sElement PRINT NEXT ' sLine
Output in the console:
Dateisystem | 1K-Blöcke | Benutzt | Verfügbar | Verw% Eingehängt auf | /dev/sda6 | 50395844 | 9446400 | 38389444 | 20% / | udev | 4037712 | 4 | 4037708 | 1% /dev | tmpfs | 1618612 | 908 | 1617704 | 1% /run | none | 5120 | 0 | 5120 | 0% /run/lock | none | 4046524 | 156 | 4046368 | 1% /run/shm | /dev/sda7 | 218656644 | 53868032 | 153681492 | 26% /home |
Based on the working method of the gb. settings component (→ 19.1 Settings), a function is presented here which can reduce a collection in the way that only those (key, value) pairs are to be retained in which the key begins with a given string (prefix).
Private Function Constrain(cColl As Collection, sPrefix As String) As Collection Dim cConstr As New Collection Dim vValue As Variant For Each vValue In cColl ' Skip elements whose key does not begin with the desired prefix. If cColl.Key Not Begins sPrefix Then Continue cConstr[cColl.Key] = vValue Next Return cConstr End
You can view the following source code as an application:
Dim cColl As Collection = ["Kontext1/S1": "wert1", "Kontext1/S2": Pi, "Kontext2/S1": "wert2"] Dim vValue As Variant For Each vValue In Constrain(cColl, "Kontext1/") Print vValue Next
Output in a console:
wert1
3.14159265358979
Here you can find the syntax for another form of the FOR-EACH control structure:
FOR EACH Expression <Instruction(s)> NEXT
Example 1 refers to an SQLite3 table in an SQLite3 database where contacts are maintained. For example, for a program test, only the first name, last name, and location of all contacts are displayed in the IDE console, although all contact data has been requested in the SQL statement:
Public Sub btnShowContacts_Click() Dim sSQL_Anweisung As String DataSource1.Table = "kontakte" MDataBase.rDBResult = Null MDataBase.cDBVerbindung.Begin sSQL_Anweisung = "SELECT * FROM " & DataSource1.Table MDataBase.rDBResult = MDataBase.cDBVerbindung.Exec(sSQL_Anweisung) MDataBase.cDBVerbindung.Commit For Each MDataBase.rDBResult Print MDataBase.rDBResult!Vorname; " "; MDataBase.rDBResult!Nachname; " - "; MDataBase.rDBResult!Wohnort Next End ' btnShowContacts_Click()
Information about the used! operator can be found in the second section of chapter '8.6 Special Operators'. The first name, last name and place of residence of all contacts are displayed formatted in the console:
Arno Adler - Aachen Bruno Bär - Berlin Heinz Hirsch - Hamburg ... Willi Wiesel - Wiesbaden Zora Zobel - Zornheim
Example 2
For example, you can store all keys used in a collection in an array:
Private Function CollectionKeys(cColl As Collection) As String[] Dim aResult As New String[] For Each cColl aResult.Add(cColl.Key) Next Return aResult End
The Collection. key property is set to the key string of the current value when the collection is enumerated. Since the listed value (from the above syntax 1) is irrelevant for this operation, the second syntax is used here. A call to the following print statement:
Dim cColl As Collection = ["test": "wert1", "schluessel": "wert2"] Print CollectionKeys(cColl).Join(", ")
… then displays:
test, schluessel