You are on page 1of 26

FOR ALL / FOR EACH

DISTINTOS MODELOS DE USO

Material tomado de PC-Soft


Marzo 2021 (Versión 25)
FOR ALL / FOR EACH

INDICE

FOR ALL/FOR EACH STATEMENT (BROWSE OF STRINGS) ...................................................................... 2


FOR EACH/FOR ALL STATEMENT (BROWSE OF XML FILE) ...................................................................... 6
FOR EACH/FOR ALL STATEMENT (BROWSE OF ARRAYS) ....................................................................... 7
FOR EACH/FOR ALL STATEMENT (BROWSE OF ASSOCIATIVE ARRAYS) ................................................ 11
FOR EACH/FOR ALL STATEMENT (BROWSE OF COMBINATIONS) ........................................................ 13
FOR EACH/FOR ALL STATEMENT (BROWSE OF CONTROLS) ................................................................. 14
FOR EACH/FOR ALL STATEMENT (BROWSE OF ENUMERATIONS) ........................................................ 17
FOR EACH/FOR ALL STATEMENT (BROWSE OF FILES) .......................................................................... 18
FOR ALL / FOR EACH

FOR ALL/FOR EACH statement (browse of strings)


In french: POUR TOUT / POUR TOUS
The FOR EACH statement is used to browse strings according to different methods:
• Browsing the sub-strings separated by a separator.
• Browsing the occurrences of a string inside another one.
Remark: The FOR ALL, FOR EACH statements are accepted. In this documentation, we will be
using the FOR EACHstatement but it can be replaced by FOR ALL.
The FOR EACH statement can also be used to browse the .Net objects that implement the
IEnumerable interface.
Example
// Retrieves the list of libraries loaded in memory
LibraryList is string = ListDLL()

// For each library
FOR EACH STRING ALibrary OF LibraryList SEPARATED BY CR
// Adds the library into the TABLE_Library table
TableAddLine(TABLE_Library, ExtractString(ALibrary, 1, TAB))
END
// The "C:\MyDocuments\Exports.TXT" file contains the list
// of exported products, separated by ";"
// Retrieve each product
ExportedProduct is string = fLoadText("C:\MyDocuments\Exports.TXT")
FormerPosition is int

// For each product
FOR EACH POSITION CurrentPosition OF ";" IN ExportedProduct
// Adds the product into ProductList
ListAdd(LIST_Product, ...
ExportedProduct[[FormerPosition + 1 TO CurrentPosition - 1]]
// Store the position
FormerPosition = CurrentPosition
END
FOR EACH STRING sc1, nPosition, nCounter OF "A.B.C" SEPARATED BY "."
Trace(sc1 + " - " + nPosition + " - " + nCounter)
END
// Returns
// A - 1 - 1
// B - 3 - 2
// C - 5 - 3
s is string = "I am a sentence"+CR+"on several lines"
FOR EACH STRING s2 OF s SEPARATED BY [" ",CR]
Trace(s2)
END

// Returns
// I
// am
// a
// sentence
// on
// several
// lines
Syntax
Browsing the sub-strings separated by a separator Hide the details
FOR ALL / FOR EACH

FOR EACH STRING <Sub-string> [, <Position> [, <Counter>]] OF <Initial String>


[SEPARATED
BY <Separator>] [<Direction>]

...
END
<FOR EACH STRING>:
Marks the beginning of statement block.
<Sub-string>:
String variable containing the text of the sub-string. There is no need to declare this variable.
<Position>:
Integer variable containing the position of the sub-string in the string. There is no need to declare
this variable.
<Counter>:
Integer variable containing the number of iterations. There is no need to declare this variable.
<Initial string>:
String containing the full text. The sub-strings are not browsed if this string is empty.
<Separator>:
Optional string containing the separator of sub-strings (TAB by default).
To specify several separators, use the following syntax:
[<Separator1> , ..., <Separator N>]
For example: [TAB, CR]
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browse the string from the first character to the last one.
(default value)

FromEnd Browse the string from the last character to the first one.

Browsing the occurrences of a string inside another string Hide the details
FOR EACH POSITION <Position> OF <Search> IN <Initial String> [<Direction>]

...
END
<FOR EACH POSITION>:
Marks the beginning of statement block.
<Position>:
Integer variable containing the current position. There is no need to declare this variable.
<Search>:
Sought string.
<Initial string>:
String containing the full text.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browse the string from the first character to the last one.
(default value)
FOR ALL / FOR EACH

FromEnd Browse the string from the last character to the first one.
Browsing the occurrences of a string inside another string Hide the details
FOR EACH POSITION <Position> OF <Search> IN <Initial String> WITH <Options>

...
END
<FOR EACH POSITION>:
Marks the beginning of statement block.
<Position>:
Integer variable containing the current position. There is no need to declare this variable.
<Search>:
Sought string.
<Initial string>:
String containing the full text.
<Options>:
Indicator for the selected options (can be combined):

FromBeginning Browse the string from the first character to the last one.
(default value)

FromEnd Browse the string from the last character to the first one.

WholeWord Search for the whole word

IgnoreCase Search while ignoring the case (uppercase/lowercase


characters)

Remarks
Syntax 1: Browsing the sub-strings separated by a separator
Browses all the sub-strings of <Initial String> separated by <Separator>. The browse is not
performed if <Initial String> is an empty string.
For each iteration:
• <Sub-string> is filled with the current sub-string.
• <Position> contains the position of the sub-string inside the string.
• <Counter> contains the number of iterations performed.
The behavior is undefined if the initial string or the separator is modified during the browse.
Remark: If <Initial String> ends with the separator, <Sub-string> returns an empty string at the
end. Otherwise, <Sub-string> corresponds to the last checked-out element.
Syntax 2 and 3: Browsing the occurrences of a string inside another one
Browses all the positions of <Search> in <Initial String>.
For each iteration, the <Position> variable is assigned with the position of the current sub-string.
The behavior is undefined if the initial string or the sought string is modified during the browse.
FOR EACH STRING, FOR EACH POSITION and UNICODE
<Sub-string>, <Initial String>, <Separator> and <Search> can correspond to:
• ANSI strings.
• UNICODE strings.
However, the ANSI strings and the UNICODE strings cannot be used in the same syntax.
See Managing the UNICODE format for more details.
FOR ALL / FOR EACH

Tip: How to get the separator?


To retrieve the separator in the loop, use the following syntax that allows you to find out the
position of the separator. To find out the separator, all you have to do is retrieve the following
character.
For example:
FOR EACH STRING sTempString, nPosition OF sStringIN SEPARATED BY [" ","-"]
sSeparator = sStringIN[[nPosition + Length(sTempString)]]
...
END

Tip: Browsing an XML string


To browse an XML file, XMLExecuteXPath must be run before using the "FOR EACH" syntax.
FOR ALL / FOR EACH

FOR EACH/FOR ALL statement (browse of XML file)


In french: POUR TOUT / POUR TOUS
The FOR EACH statement is used to perform different types of browse on an xmlReader variable:
• Browse on a level.
• Browse on a level with copy.
• Browse in depth.
Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
Example
< ?xml version="1.0"?>
<aa>
<bb>
Text1

<cc>
</cc>
</bb>
<bb>
<cc>
</cc>
</bb>
</aa>
// Browse on a level
// Reader is the variable corresponding to the XML file
FOR EACH Reader
// Read the start aa and end aa tags
FOR EACH Reader
// Read the start bb, end bb, start bb, end bb tags
FOR EACH Reader
// Read Text1 and the start cc, end cc
// start cc, end cc tags
END
END
END
// Reader is the variable corresponding to the XML file
FOR EACH Reader IN DEPTH
// get tags start aa, start bb, text1,
// start cc, end cc, end bb, start bb,
// start cc, end cc, end bb, end aa tags
END
Syntax
Browsing the elements of XML file on a level Hide the details
FOR EACH <XML Reader>
FOR EACH <XML Reader>
...
END
END
<FOR EACH>:
Marks the beginning of statement block.
Used to browse the children of current level.
<XML Reader>:
xmlReader variable corresponding to the XML file to browse.
Inside the loop, the xmlReader variable points to the current XML element.
FOR ALL / FOR EACH

Remark: You have the ability to nest browses on the XML reader.
Browse on a level with copy Hide the details
FOR EACH <Element> OF <XML Reader>
FOR EACH <Element A> OF <Element>
...
END
END
<FOR EACH>:
Marks the beginning of statement block.
Used to browse the children of current level.
<Element>:
Inside the loop, <Element> points to the current XML element. It can be used to perform a new FOR
EACH loop to browse its children.
<XML Reader>:
xmlReader variable corresponding to the XML file to browse.
Browse in depth Hide the details
FOR EACH <XML Reader> IN DEPTH
...
END

FOR EACH <Element> OF <XML Reader> IN DEPTH


...
END
<FOR EACH>:
Marks the beginning of statement block.
Used to browse the XML treeview in depth: browse the child, then the grandchild, then the child of
grandchild until a leaf is reached.
<Element>:
Inside the loop, <Element> points to the current XML element. Used to perform a reading in depth
from an element.
<XML Reader>:
xmlReader variable corresponding to the XML file to browse.
Remarks
Syntax 1: Browsing the elements of XML file on a level
This syntax is used to browse the children of current level. Inside the loop, the xmlReader variable
points to the current XML element.
You have the ability to nest browses on the xmlReader variable in order to browse the children of
element on the level currently browsed.
Syntax 2: Browse on a level with copy
This syntax is used to browse the children of current level. Inside the loop, <Element> points to the
current XML element and it is possible to use it to perform another browse on its own children.
Remark: A single browse can be performed on the <Element> variable.
Syntax 3: Browse in depth
This syntax is used to browse the XML treeview in depth, which means the child, then the
grandchild, ... until a leaf is reached. When the leaf is reached, the loop goes to the next child.

FOR EACH/FOR ALL statement (browse of arrays)


In french: POUR TOUT / POUR TOUS
FOR ALL / FOR EACH

FOR EACH is used to perform different types of browse on the arrays:


• Browsing the array elements,
• Browsing the values of array elements.
Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
The FOR EACH statement can also be used to browse the .Net objects that implement the
IEnumerable interface.
Example
// Browse the elements found in an array of reals and calculate the sum
// Fill the array
ArrCalc is array of 3 reals
ArrCalc[1] = 12.5
ArrCalc[2] = 10
ArrCalc[3] = 7.5
// Calculate the sum
TotalSum is real
FOR EACH AnElement OF ArrCalc
TotalSum += AnElement
END


// Browse the arrCustomers array and only process the customers
// whose city is equal to "MONTPELLIER".

stCustomer is Structure
Name is string
FirstName is string
City is string
END

arrCustomers is array of stCustomer

FOR EACH stACustomer OF arrCustomers WHERE City = "MONTPELLIER"
// Process
END
Syntax
Browsing the array elements Hide the details
FOR EACH [ELEMENT] <Variable> [, <Key> [, <Counter>]] OF <Array> [WHERE
<Condition>] [<Direction>]
...
END
<FOR EACH [ELEMENT]>:
Marks the beginning of statement block. The ELEMENT keyword is optional.
<Variable>:
Variable whose type is identical to the type of the array elements. For the arrays of classes, the
variable must be a Dynamic Class variable. There is no need to declare this variable.
<Key>:
Key of element browsed. This key depends on the element browsed:
• One-dimensional array: subscript of the element in the array.
• Two-dimensional array (or n-dimensional array): counter that starts from 1
There is no need to declare this variable.
<Counter>:
FOR ALL / FOR EACH

Integer variable containing the number of iterations. There is no need to declare this variable.
<Array>:
Array to browse.
<Condition>:

Condition to indicate to filter the browse. Only the array elements


corresponding to the filter will be browsed.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browse the array from the first element to the last one.
(default value)

FromEnd Browse the array from the last element to the first one.

Browsing the values of array elements Hide the details


FOR EACH [ELEMENT] <Value> OF <Array> [WHERE <Condition>] [<Direction>]
...
END
<FOR EACH ELEMENT>:
Marks the beginning of statement block. The ELEMENT keyword is optional.
<Value>:
Variable whose type is compatible with the array elements. There is no need to declare this
variable.
<Array>:
Array to browse.
<Condition>:

Condition to indicate to filter the browse. Only the array elements


corresponding to the filter will be browsed.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browse the array from the first element to the last one.
(default value)

FromEnd Browse the array from the last element to the first one.

Remarks
Syntax 1: Browsing the array elements
For each iteration, <Variable> directly refers to the current element in the array. If the value of
<Variable> is modified, the current element in the array is modified.
When exiting from the loop (standard exit or via the BREAK statement), the value of the last
element read is assigned to <Variable> but <Variable> does not directly refer to the array element
anymore.
All types of arrays are available: automatic, fixed, dynamic.
The arrays can have several dimensions.
The behavior is undefined if the number of elements is modified in the browse loop.
Syntax 2: Browsing the values of array elements
FOR ALL / FOR EACH

For each iteration, the value of the element browsed is assigned to the <Value> variable. If the value
of <Value> is modified, the current element in the array is not modified.
All types of arrays are available: automatic, fixed, dynamic.
The arrays can have several dimensions.
The behavior is undefined if the number of elements is modified in the browse loop.
FOR ALL / FOR EACH

FOR EACH/FOR ALL statement (browse of associative arrays)


In french: POUR TOUT / POUR TOUS
FOR EACH is used to perform different types of browse operations on the associative arrays:
• Browsing the elements of associative array.
• Browsing the values of the elements found in the associative array.
Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
The FOR EACH statement can also be used to browse the .Net objects that implement the
IEnumerable interface.
Example
// Declare an associative array of integers
// Array indexed on strings and without duplicates
aaIDPerCustomer is associative array of int
aaIDPerCustomer["A"] = 55 // Add the identifier of customer "A"
aaIDPerCustomer["B"] = 321 // Add the identifier of customer "B"
aaIDPerCustomer["A"] = 56 // Modify the identifier of customer "A"
// Browse all the identifiers (syntax 1)
// 56
// 321
FOR EACH nIdentifier OF aaIDPerCustomer
Trace(nIdentifier)
END
// Declare an associative array of integers
// This array is indexed on strings with duplicates
// Ignores the case and the accented characters
aaIDPerCustomer is associative array (WithDuplicates + ccIgnoreCasse + ccIgnoreAccent)
of integers
aaIDPerCustomer["E"] = 55 // add the identifier of customer "E"
aaIDPerCustomer["B"] = 321 // add the identifier of customer "B"
aaIDPerCustomer["e"] = 8 // add the identifier of customer "e"
aaIDPerCustomer["é"] = 127 // add the identifier of customer "é"
// Browse all the identifiers of customers "E" (Syntax 2)
// 55
// 8
// 127
FOR EACH nIdentifier OF aaIDPerCustomer = "E"
Trace(nIdentifier)
END
Syntax
Browsing the array elements Hide the details
FOR EACH [ELEMENT] <Variable> [, <Key> [, <Counter>]] OF <Array> [WHERE
<Condition>] [<Direction>]
...
END
<FOR EACH [ELEMENT]>:
Marks the beginning of statement block. The ELEMENT keyword is optional.
<Variable>:
Variable whose type is identical to the type of the array elements. For the arrays of classes, the
variable must be a Dynamic Class variable. There is no need to declare this variable.
Remark: This variable is a reference to the array value. A modification of this variable in FOR EACH
will also modify the value in the associative array.
<Key>:
FOR ALL / FOR EACH

Key of element browsed. There is no need to declare this variable.


<Counter>:
Integer variable containing the number of iterations. There is no need to declare this variable.
<Array>:
Array to browse.
<Condition>:

Condition to indicate to filter the browse. Only the array elements


corresponding to the filter will be browsed.
<Direction>:
Optional indicator for the browse direction.

FromBeginning Browse the array in the order of additions into the array.
(default value)

FromEnd Browse the array in the reverse order of additions into the
array.
Browsing the values of the "Key" elements found in the array Hide the details
FOR EACH [ELEMENT] <Variable> OF <Array> = <Key> [WHERE <Condition>]
[<Direction>]
...
END
<FOR EACH ELEMENT>:
Marks the beginning of statement block. The ELEMENT keyword is optional.
<Variable>:
Variable whose type is identical to the type of the array elements. For the arrays of classes, the
variable must be a Dynamic Class variable. There is no need to declare this variable.
Remark: This variable is a reference to the array value. A modification of this variable in FOR EACH
will also modify the value in the associative array.
<Array>:
Array to browse.
<Key>:
Value of the key for which the array elements must be browsed. For an associative array without
duplicates, 0 or 1 element will be browsed. For an associative array with duplicates, 0 or N
elements will be browsed.
<Condition>:

Condition to indicate to filter the browse. Only the array elements


corresponding to the filter will be browsed.
<Direction>:
Optional indicator for the browse direction.

FromBeginning Browse the array in the order of additions into the array.
(default value)

FromEnd Browse the array in the reverse order of additions into the
array.

Remarks
FOR ALL / FOR EACH

Syntax 1: Browsing the array elements


For each iteration, <Variable> directly refers to the current element in the array. If the value of
<Variable> is modified, the current element in the array is modified.
When exiting from the loop (standard exit or via the BREAK statement), the value of the last
element read is assigned to <Variable> but <Variable> does not directly refer to the array element
anymore.
Example:
// Declare an associative array of integers
// Array indexed on strings and without duplicates
aaIDPerCustomer is associative array of int
aaIDPerCustomer["A"] = 55 // Add the identifier of customer "A"
aaIDPerCustomer["B"] = 321 // Add the identifier of customer "B"
aaIDPerCustomer["A"] = 56 // Modify the identifier of customer "A"
// Browse all the identifiers
// 56
// 321
FOR EACH nIdentifier OF aaIDPerCustomer
Trace(nIdentifier)
END
Syntax 2: Browsing the values of the "Key" elements found in the array
This syntax browses all the array elements with the specified <Key> value. For each iteration,
<Variable> directly refers to the current element in the array. If the value of <Variable> is modified,
the current element in the array is modified.
This syntax is useful when browsing associative arrays with duplicates. In an associative array
without duplicates, the number of elements browsed can be 0 or 1. In an associative array with
duplicates, the number of elements browsed can be 0 or N.
The array elements are browsed in the order of additions (no direction option).
When exiting from the loop (standard exit or via the BREAK statement), the value of the last
element read is assigned to <Variable> but <Variable> does not directly refer to the array element
anymore.
Example:
// Declare an associative array of integers
// This array is indexed on strings with duplicates
// Ignores the case and the accented characters
aaIDPerCustomer is associative array (WithDuplicates + ccIgnoreCasse + ccIgnoreAccent)
of integers
aaIDPerCustomer["E"] = 55 // add the identifier of customer "E"
aaIDPerCustomer["B"] = 321 // add the identifier of customer "B"
aaIDPerCustomer["e"] = 8 // add the identifier of customer "e"
aaIDPerCustomer["é"] = 127 // add the identifier of customer "é"
// Browse all the identifiers of customers "E"
// 55
// 8
// 127
FOR EACH nIdentifier OF aaIDPerCustomer = "E"
Trace(nIdentifier)
END

FOR EACH/FOR ALL statement (browse of combinations)


In french: POUR TOUT / POUR TOUS
The FOR EACH statement is used to browse the available values in the combinations.
FOR ALL / FOR EACH

Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
Example
// Declaration code
// Declare a combination
CombinationType is Combination
CreateFile
ReadFile
WriteFile
DeleteFile
END
// Syntax 1: Browse the combination options
FOR EACH eOption OF CombinationType
Trace(eOption..Name)
END
// Assign the variable
FileAction = ReadFile + WriteFile

// Syntax 2: Browse the active options
FOR EACH eOption OF FileAction
Trace(eOption..Name)
END
Syntax
Simplified syntax Hide the details
FOR EACH <MyOption> OF <Combination>
...
END
<FOR EACH>:
Marks the beginning of statement block.
<MyOption>:
Option Description variable corresponding to the combination option. There is no need to declare
this variable.
<Combination>:
• Combination containing the values to browse.
• Combination variable containing the options to browse. All the options (active or inactive)
are browsed.
Browsing the active options of a Combination variable Hide the details
FOR EACH <MyOption> OF <Combination>
...
END
<FOR EACH>:
Marks the beginning of statement block.
<MyOption>:
Option Description variable corresponding to the combination option. There is no need to declare
this variable.
<Combination>:
Combination variable containing the options to browse. Only the active options are browsed.

FOR EACH/FOR ALL statement (browse of controls)


In french: POUR TOUT / POUR TOUS
FOR ALL / FOR EACH

The statement FOR EACH is used to perform different types of browse on the controls (List Box,
Table or Looper controls):
• Browsing the control elements.
• Browsing the values of the selected elements.
• Browsing the subscripts of the selected elements.
Remark: The FOR ALL, FOR EACH statements are accepted. In this documentation, we will be
using the FOR EACHstatement but it can be replaced by FOR ALL.
Limitation: This statement cannot be used on the Combo Box controls.
The FOR EACH statement can also be used to browse the .Net objects that implement the
IEnumerable interface.
Example
// For each user selected in TABLE_User
FOR EACH SELECTED ROW OF TABLE_User
// Send an email
Proc_SendMessage(COL_Name, COL_Email)
END
Syntax
Browsing the control elements (Table, List Box and Looper) Hide the details
FOR EACH ROW [<Subscript> [, <Counter>]] OF <Control>
...
END
<FOR EACH ROW>:
Marks the beginning of statement block.
<Subscript>:
Optional Integer variable. For each iteration, contains the subscript of the current row.
<Counter>:
Optional Integer variable. This variable counts the number of iterations.
<Control>:
Control to browse: List Box, Table or Looper control.

The Table controls are not available.


Browsing the selected elements Hide the details
FOR EACH SELECTED ROW [<Subscript>, [<Counter>]] OF <Control>

...
END
<FOR EACH SELECTED ROW>:
Marks the beginning of statement block.
<Subscript>:
Integer variable. For each iteration, contains the subscript of the current selected row.
<Counter>:
Optional Integer variable. This variable counts the number of iterations (number of selected rows
for example).
<Control>:
Control to browse: List Box, Table or Looper control.

The Table controls are not available.


Remarks
Syntax 1: Browsing the control elements
FOR ALL / FOR EACH

This syntax is used to browse all the rows, visible or not, of a List Box, Table or Looper control.
For each row browsed:
• <Control name> returns the subscript of the current row. <Subscript> can also be used to
find out the subscript of the current row.
• <Control name>[<Control name>] returns all the columns separated by TAB characters.
This syntax is equivalent to <Control name>[<Subscript>].
• <Column name> returns the column value for the row currently browsed.
The browse has no effect on the current selection.
The behavior is undefined if the number of control elements is modified during the browse.

Syntax 2: Browsing the selected control elements


This syntax is used to browse all the selected rows, visible or not, of a List Box, Table or Looper
control.
For each row browsed:
• <Control name> returns the subscript of the current selected row. <Subscript> can also be
used to find out the subscript of the current selected row.
• <Control name>[<Control name>] returns all the columns separated by TAB characters.
• <Column name> returns the column value for the row currently browsed. You can also use
the following syntax: <Column>[<Subscript>]
Change of behavior in version 12: Before version 12, the browse operation had no influence on
the current selection. From now on, the current row in the table is always moved during the
browse.
The behavior is undefined if the number of control elements is modified during the browse.

Browsing the rows of a Table control


When browsing the rows of a Table control:
• the display of this Table control is locked. MultitaskRedraw is ignored.
• the selected rows and/or the current row must not be modified
(TableSelectMinus, TableSelectPlus, ...).
• for a browsing Table control, in the browse loop, the current record is the record
processed by the browse.

Reminder: The Table controls are not available.


FOR ALL / FOR EACH

FOR EACH/FOR ALL statement (browse of enumerations)


In french: POUR TOUT / POUR TOUS
The FOR EACH statement is used to browse the available values in the enumerations.
Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
Example
// Browse the enumeration options
// ---------------------------------------
// Declaration code
// Declare an enumeration
RobotType is Enumeration
Walk
Stopped
InMaintenance
OutOfOrder
END
// Browse the values
FOR EACH Option OF RobotType
Trace(Option..Name)
END
Syntax
FOR EACH <MyValue> OF <Enumeration>
...
END
<FOR EACH>:
Marks the beginning of statement block.
<MyValue>:
Value description variable corresponding to the enumeration value. There is no need to declare this
variable.
<Enumeration>:
Enumeration containing the values to browse.
FOR ALL / FOR EACH

FOR EACH/FOR ALL statement (browse of files)


In french: POUR TOUT / POUR TOUS
Not available with this kind of connection
The FOR EACH statement is used to perform different types of HFSQL browse:
• Full browse (according to a specified key or not)
• Browse with filter (simple filter, filter on a key or filter on the search key). In this case, the
filters accept the operators of HFilter.
The browse operations can be performed on a data file, a view, a query or a data source. The
records locked in read/write are not read.
Remarks:
• If the key given to the FOR EACH statement is the key that was previously returned
by HFilter, the filter will be respected.
• The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used
in this documentation but it can be replaced by FOR ALL.
Example
// Syntax 1
FOR EACH Customer
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 1
FOR EACH Customer on CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 2
// Browse with filter
FOR EACH Customer WHERE "CustomerCity = 'Montpellier'"
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END

City = "Lyon"
FOR EACH Customer WHERE "CustomerCity = '"+City+"' and CustomerAge >= 21"
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 2
// Browse with filter on a specific key
FOR EACH Customer WHERE "CustomerCity = 'Montpellier'" on CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END

City = "Lyon"
FOR EACH Customer WHERE "CustomerCity = '" + City + "' AND " + ...
"CustomerAge >= 21" on CustomerNum
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 3
// Comparison according to a value
FOR EACH Customer WHERE CustomerName = "Doe"
FOR ALL / FOR EACH

// Add customers into the List Box control


ListAdd(LIST_CustomerList, Customer.CustomerNum)
END

// Comparison according to a value
FOR EACH Orders WHERE OrderDate = "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END
// Syntax 3
// Comparison according to a set of values
FOR EACH Orders WHERE OrderDate = "20030101" TO "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END

// Comparison according to a set of values
FOR EACH Orders WHERE "20030101" <= OrderDate <= "20031231"
// Add orders into the List Box control
ListAdd(LIST_OrderList, Order.OrderNum)
END
// Syntax 4
// Browse with filter on a specific key
FOR EACH Customer WHERE CityCustomer [= "Mont" FromEnd
// Add customers into the List Box control
ListAdd(LIST_CustomerList, Customer.CustomerNum)
END
// Syntax 5
// Browse with filter on a composite key
FOR EACH ContactFile WHERE CCState = ["Prospect", 69]
// Add contacts into the List Box control
ListAdd(LIST_ContactList, ContactFile.ContactNum)
END

// Browse with filter on a composite key
FOR EACH ContactFile WHERE CZipCode [= ["Prospect", 69]
// Add contacts into the List Box control
ListAdd(LIST_ContactList, ContactFile.ContactNum)
END
Syntax
1 - Full browse Hide the details
1. Full browse according to the best search key

Caution: Modifying the file in the analysis (adding a key item for example)
can modify the search key used.

FOR EACH <File> [[WITHOUTSAVEPOSITION,] [<Direction>]]


...
END

2. Full browse according to the key passed in parameter

FOR EACH <File> ON <Key item> [[WITHOUTSAVEPOSITION,] [<Direction>]]


...
END
<FOR EACH>:
Marks the beginning of statement block.
FOR ALL / FOR EACH

<File>:
Name of the HFSQL data file, view or query to browse.
<ON>:
Defines the type of browse.
<Key item>:
Name of the key item used to browse the HFSQL data file (view or query).
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves
the position at the beginning of the loop and restores this position in case of "normal" exit from the
loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or
RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browses the data file from the first element to the last one.
(default value)

FromEnd Browses the data file from the last element to the first one.

<END>:
Marks the end of statement block.
2 - Browse with filter Hide the details
1. Browse with filter (the best search key is automatically defined)

Caution: Modifying the file in the analysis (adding a key item for example)
can modify the search key used.

FOR EACH <File> WHERE "<1st Condition>


[AND/OR/NOT <2nd Condition>
[ET/OU/PAS...<Nth Condition>]]" [[WIHTOUTSAVEPOSITION,]
[<Direction>]]
...
END

2. Browse with filter according to the specified key

FOR EACH <File> WHERE "<1st Condition>


[AND/OR/NOT <2nd Condition>
[ET/OU/PAS...<Nth Condition>]]" [[WIHTOUTSAVEPOSITION,]
[<Direction>]]
...
ON <Key Item>
END
<FOR EACH>:
Marks the beginning of statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
FOR ALL / FOR EACH

Defines the type of browse.


<Nth Condition>:
Nth condition of the filter for the HFSQL browse. See the Remarks for more details.
<AND/OR/NOT>:
Optional logical operators used to combine the different filter conditions.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves
the position at the beginning of the loop and restores this position in case of "normal" exit from the
loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or
RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browses the data file from the first element to the last one.
(default value)

FromEnd Browses the data file from the last element to the first one.

<Key item>:
Item corresponding to a key of the data file. The filter will be performed on this key.
<END>:
Marks the end of statement block.
3 - Browse with selection filter on the search key Hide the details
1. Comparison filter according to a value

FOR EACH <Fie> WHERE <Key item> [ = / <= / >= ] <Value>


[[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Comparison filter according to an interval of values

FOR EACH <File> WHERE <Key item> = <Minimal value> TO <Maximum value>
[[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END

FOR EACH <File> WHERE <Minimum value> <= <Key item> <= <Maximum value>
[[WIHTOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
Defines the type of browse.
<Key item>:
FOR ALL / FOR EACH

Key item of the data file to browse.


<Value>:
Comparison value of the key item.
<Minimum Value>, <Maximum Value>:
Comparison value of the key item.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves
the position at the beginning of the loop and restores this position in case of "normal" exit from the
loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or
RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browses the data file from the first element to the last one.
(default value)

FromEnd Browses the data file from the last element to the first one.

<END>:
Marks the end of statement block.
4 - Browse with "Start with" generic search, ascending or descending Hide the details
FOR EACH <File> WHERE <Key item> [= <Beginning of sought value>
[[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END
<FOR EACH>:
Marks the beginning of statement block.
<File>:
Name of the HFSQL data file, view or query to browse.
<WITH>:
Defines the type of browse.
<Key item>:
Key item of the data file to browse.
<Beginning of sought value>:
Comparison value of the key item.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves
the position at the beginning of the loop and restores this position in case of "normal" exit from the
loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or
RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:
FOR ALL / FOR EACH

FromBeginning Browses the data file from the first element to the last one.
(default value)

FromEnd Browses the data file from the last element to the first one.

<END>:
Marks the end of statement block.
5 - Browse with filter on a composite key Hide the details
1. Exact-match search

FOR EACH <File> WHERE <Composite key> = [<Value of component 1>, ..., <Value
of component N>] [[WITHOUTSAVEPOSITION,] [<Direction>]]
...
END

2. Generic search ("Starts with")

FOR EACH <File> WHERE <Composite key> [= [<Value of component 1>, ..., <Value
of component N>] [[WITHOUTSAVEPOSITION,] [<Direction>]]

...
END
<FOR EACH>:
Marks the beginning of statement block.
<File>:
Name of the HFSQL data file or view to browse.
<WITH>:
Defines the type of browse.
<Composite key>:
Composite key of the data file to browse.
<Value of Component N>:
Value of the component.
<WITHOUTSAVEPOSITION>:
Optional keyword.
Disables the position save and restore during the browse. Indeed, FOR EACH automatically saves
the position at the beginning of the loop and restores this position in case of "normal" exit from the
loop (the position is not restored if the loop is interrupted by the keywords BREAK, RETURN or
RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
<Direction>:
Optional indicator for the browse direction:

FromBeginning Browses the data file from the first element to the last one.
(default value)

FromEnd Browses the data file from the last element to the first one.
<END>:
Marks the end of statement block.
Remarks
FOR ALL / FOR EACH

Equivalence
• The syntax FOR EACH <File> WHERE <Item> = <Value> is equivalent to a browse
performed on the data file by HReadSeek. This browse takes the filters previously defined
on the data file into account.
• The syntax FOR EACH <File> WHERE <Condition> is used to define a filter and to browse
the data file. This browse ignores the filters that were defined beforehand.
• When using queries, the "FOR EACH" syntax may be slower than the "WHILE NOT HOut"
syntax. Indeed, when using FOR EACH, an operation for saving/restoring context is
automatically performed (equivalent to HSavePosition/HRestorePosition).
Filter (syntaxes 2)
The general syntax of a filter has the following format:
"<Item Name> <Operators> <Item Value>"
For example:
"CustomerName > 'Doe' and ZipCode = 75 or CustomerAge >= 32"
The supported operators depend on the type of items used in the condition:

<> Different Valid for all types

> Greater than Valid for all types

>= Greater than or equal to Valid for all types

< Less than Valid for all types

<= Less than or equal to Valid for all types

= Strictly equal to Valid for all types

~= Almost equal to Valid for the "string" types only

] Contains Valid for the "string" types only

]= Starts with Valid for the "string" types only


Remarks:
• Constant strings must be enclosed in single quotes. For example: "CustomerName =
'"+Customer+"'"
• <Item name> must only contain letters, digits and underscore characters ("_"). If <Item
name> contains other characters (quote, ...), the name of the item must be enclosed in
double quotes. For example: "e_mail@"]'com'
• Comparisons between strings are performed according to the ASCII value of the characters
and not according to the lexicographic value ('a' > 'Z').
• The binary memos and the composite keys cannot be part of an <Item Value>.
• If <Item Value> contains a simple quote (or a double quote), this simple quote (or double
quote) must be preceded by a backslash character.
Exiting from FOR EACH loop
Several statements are available:
• RETURN: Exit from the FOR EACH loop and exit from the current process (or procedure).
• RESULT: Return a status report to the calling process. Exit from the FOR EACH loop and
exit from the current process (or procedure).
FOR ALL / FOR EACH

• BREAK: Exits from the FOR EACH loop and runs the rest of the current process.
Close is used to exit from the FOR EACH loop and to close the current window.
Caution: RETURN and RESULT cannot be used in the same process.

Influence of the mode for exiting from the loop on the automatic browse of data files
• If the browse ends automatically:
• The position in the data file before the browse is restored.
• The possible filter required for the browse is disabled.
• If the browse is interrupted (BREAK, RETURN, RESULT, Close, ...):
• The position in the data file before the browse is not restored.
• The possible filter required for the browse is not disabled. It must be disabled
manually (HDeactivateFilter).
If the keyword WIHTOUTSAVEPOSITION has been used, the position save and restore operations
performed during the browse are disabled. Indeed, FOR EACH automatically saves the position at
the beginning of the loop and restores this position in case of "normal" exit from the loop (the
position is not restored if the loop is interrupted by the keywords BREAK, RETURN or RESULT).
These position save and restore operations can have a high impact (mainly in terms of time),
especially if the code using the instruction FOR EACH itself is called in a loop.
Running the next iteration
To directly run the next iteration without ending the code of the next iteration, use the
statement Continue:
// Browse FileName in the order of KeyItem
FOR EACH FileName on KeyItem
...
IF Condition = True THEN CONTINUE // Return to the FOR EACH keyword
// and go to the next record
...
END

Modifying the key used for the automatic browse of data files or queries
If the value of the search item is modified when browsing a data file (or a query), some records may
be browsed several times.
Indeed, modifying the browse item updates the file index key. This modification is taken into
account during the automatic reading of the next records.
This remark is also valid for the automatic browse of a sorted query (ORDER BY) without search
key.
// Browse FileName on KeyItem
FOR EACH FileName on KeyItem
...
IF Condition = True THEN
// Modify the value of the search key
FileName.KeyItem = "New value"
HModify(FileName)

END
// If the new value of the search key
// positions on the record after in the browse order
// the record will be read again during the browse.
...
END

You might also like