|
|
|
|
|
- Overview
- Method 1: Read functions
- Code example
- Method 2: FOR EACH statement
- Code example
- Method 3: HFilter function
- Example
- Method 4: Using an SQL query
- Example
How to loop through a file using filters?
There are multiple methods to loop though a data file using filters: This method uses the following WLanguage functions to loop through data files: HReadSeek is used to access the first record corresponding to the value of the minimum bound for the search key. HReadNext reads the next record that matches the search. HFound checks whether there is a record that matches the search value. Code example // Loop through the records of the Customer file whose city is PARIS.  HReadSeek(CUSTOMER, City, "PARIS") WHILE HFound(CUSTOMER) // Process the CUSTOMER record  HReadNext(CUSTOMER, City) END
Method 2: FOR EACH statement The FOR EACH statement loops through the records of a data file. In our case, this statement will be used to read the records found in a data file according to a filter. The FOR EACH statement expects the following parameters: - the name of the data file to loop through.
- the name of the index (or key) used to sort the records.
- the filtering value.
The first record and the next record are read by the FOR EACH statement. There is no need to use the HReadXXX functions. Code example // Loop through the records of the Customer file whose city is PARIS. FOR EACH CUSTOMER where CITY = "PARIS" // Process the CUSTOMER record END
Method 3: HFilter function - Use HFilter to apply a filter on the records of the data file.
- Loop through the filtered data file using the standard read functions.
- At the end of the operation, disable the filter with HDeactivateFilter.
Example Find orders whose date is between 02/01/2017 and 02/28/2017. // Apply the filter HFilter(ORDER, ORDERDATE, "20170201", "20170228") Â // Loop through the file FOR EACH ORDER // Process the order read END Â Â // Disable filter HDeactivateFilter(ORDER)
Note: HFilter is easier to use than the previous methods. It can also be used to manage more filter capabilities. For more details, see the online help about HFilter. Method 4: Using an SQL query To loop through the data file using a filter via a query: - Create the query. The query is used to filter the requested records.
Reminder: An SQL query can be performed: - in the query editor.
- programmatically.
For more details on how to create a query, see Creating a query. - Execute the query (HExecuteQuery or HExecuteSQLQuery).
- Read the query result by looping through the data file.
- Free the query (HFreeQuery).
Example HExecuteQuery(QRY_CustomerList) FOR EACH QRY_CustomerList // Process the customer read in the query END HFreeQuery(QRY_CustomerList)
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|