4D v17.4For each...End for each |
||||||||||||||||||||||
|
4D v17.4
For each...End for each
|
Loop through collections | Loop through entity selections | Loop through objects | |
Current_Item type | Variable of the same type as collection elements | Entity | Text variable |
Expression type | Collection (with elements of the same type) | Entity selection | Object |
Number of loops (by default) | Number of collection elements | Number of entities in the selection | Number of object properties |
Support of begin / end parameters | Yes | Yes | No |
When For each...End for each is used with an Expression of the Collection type, the Current_Item parameter is a variable of the same type as the collection elements. By default, the number of loops is based on the number of items of the collection.
The collection must contain only elements of the same type, otherwise an error will be returned as soon as the Current_Item variable is assigned the first mismatched value type.
At each loop iteration, the Current_Item variable is automatically filled with the matching element of the collection. The following points must be taken into account:
You want to compute some statistics for a collection of numbers:
C_COLLECTION($nums)
$nums:=New collection(10;5001;6665;33;1;42;7850)
C_LONGINT($item;$vEven;$vOdd;$vUnder;$vOver)
For each($item;$nums)
If($item%2=0)
$vEven:=$vEven+1
Else
$vOdd:=$vOdd+1
End if
Case of
:($item<5000)
$vUnder:=$vUnder+1
:($item>6000)
$vOver:=$vOver+1
End case
End for each
//$vEven=3, $vOdd=4
//$vUnder=4,$vOver=2
When For each...End for each is used with an Expression of the Entity selection type, the Current_Item parameter is the entity that is currently processed.
The number of loops is based on the number of entities in the entity selection. On each loop iteration, the Current_Item parameter is automatically filled with the entity of the entity selection that is currently processed.
Note: If the entity selection contains an entity that was removed meanwhile by another process, it is automatically skipped during the loop.
Keep in mind that any modifications applied on the current entity must be saved explicitly using entity.save( ).
You want to raise the salary of all British employees in an entity selection:
C_OBJECT(emp)
For each(emp;ds.Employees.query("country='UK'"))
emp.salary:=emp.salary*1,03
emp.save()
End for each
When For each...End for each is used with an Expression of the Object type, the Current_Item parameter is a text variable automatically filled with the name of the currently processed property.
The properties of the object are processed according to their order of creation. During the loop, properties can be added to or removed from the object, without modifying the number of loops that will remain based on the original number of properties of the object.
You want to switch the names to uppercase in the following object:
{ "firstname": "gregory", "lastname": "badikora", "age": 20 }
You can write:
For each(property;vObject)
If(Value type(vObject[property])=Is text)
vObject[property]:=Uppercase(vObject[property])
End if
End for each
{ "firstname": "GREGORY", "lastname": "BADIKORA", "age": 20 }
You can define bounds to the iteration using the optional begin and end parameters.
Note: The begin and end parameters can only be used in iterations through collections and entity selections (they are ignored on object properties).
If end is omitted or if end is greater than the number of elements in Expression, elements are iterated from begin until the last one (included).
If the begin and end parameters are positive values, they represent actual positions of elements in Expression.
If begin is a negative value, it is recalculed as begin:=begin+Expression size (it is considered as the offset from the end of Expression). If the calculated value is negative, begin is set to 0.
Note: Even if begin is negative, the iteration is still performed in the standard order.
If end is a negative value, it is recalculed as end:=end+Expression size
For example:
C_COLLECTION($col;$col2)
$col:=New collection("a";"b";"c";"d";"e")
$col2:=New collection(1;2;3)
C_TEXT($item)
For each($item;$col;0;3)
$col2.push($item)
End for each
//$col2=[1,2,3,"a","b","c"]
For each($item;$col;-2;-1)
$col2.push($item)
End for each
//$col2=[1,2,3,"a","b","c","d"]
You can control the For each...End for each execution by adding an Until or a While condition to the loop. When an Until(condition) or a While(condition) statement is associated to the loop, the iteration will stop as soon as the condition is evaluated to true.
You can pass either keyword depending on your needs:
$colNum:=New collection(1;2;3;4;5;6;7;8;9;10)
$total:=0
For each($num;$colNum)While($total<30) //tested at the beginning
$total:=$total+$num
End for each
ALERT(String($total)) //$total = 36 (1+2+3+4+5+6+7+8)
$total:=1000
For each($num;$colNum)Until($total>30) //tested at the end
$total:=$total+$num
End for each
ALERT(String($total)) //$total = 1001 (1000+1)
The For each...End for each loop can be used on a shared collection or a shared object.
If your code needs to modify one or more element(s) of the collection or object properties, you need to use the Use...End use keywords. Depending on your needs, you can call the Use...End use keywords:
Product: 4D
Theme: Language definition
Created: 4D v17
4D Language Reference ( 4D v17)
4D Language Reference ( 4D v17.1)
4D Language Reference ( 4D v17.2)
4D Language Reference ( 4D v17.3)
4D Language Reference ( 4D v17.4)