π Introduction
As a Uniface developer, you frequently encounter the need to process lists. The forlist...endfor
loop is a powerful tool in Uniface 10.4, designed exactly for this purpose. In this post, I'll explain how to use this loop effectively! π
π― What is forlist...endfor?
The forlist...endfor
statement defines a loop that processes all items in an indexed list. It's available in all Uniface component types and provides an elegant solution for list processing.
π Syntax
forlist Item {, Index} in SourceList
Your ProcScript
endfor
π§ Parameters in Detail
Parameter | Data Type | Description |
---|---|---|
Item | String | Current list item |
Index | Number | Item number in list |
SourceList | String | Variable or field containing Uniface (Gold-separated) list |
βοΈ How it Works
The loop functions as follows:
- π Iteration: Each time the loop reaches
endfor
, Item and Index (if defined) are incremented - π Termination Conditions: The loop stops when:
- Item number > last item number
- Index > last item number
- A
break
statement is encountered
- π Index Value After Loop: Index = last item number + 1 (or position at break)
π‘ Practical Example
Here's a concrete example that searches for "Pompey" in a list of ancient cities:
variables
string vList
string vItem
numeric vIndex
endvariables
vList = "Athens;Rome;Syracuse;Pompey;Sparta"
forlist vItem, vIndex in vList
if (vItem = "Pompey")
putmess "Loop processing stopped on Item number: %%vIndex, Value: %%vItem"
break
endif
putmess "Processing Item number: %%vIndex, Value: %%vItem"
endfor
π€ Output:
Processing Item number: 1, Value: Athens
Processing Item number: 2, Value: Rome
Processing Item number: 3, Value: Syracuse
Loop processing stopped on Item number: 4, Value: Pompey
π― Best Practices
β Do's:
- Manipulate Index: You can change the Index within the loop for conditional control
-
Use Break: Utilize
break
for early termination when finding values -
Meaningful Variable Names:
vItem
andvIndex
are self-explanatory
β Don'ts:
- Avoid Infinite Loops: Pay attention to correct termination conditions when manipulating Index
- Consider Performance: For very large lists, consider alternative approaches
π Advanced Use Cases
π Search and Replace:
forlist vCurrentItem, vCurrentIndex in vDataList
if (vCurrentItem = vSearchValue)
; Replace or process found value
break
endif
endfor
π Data Validation:
forlist vItem, vIndex in vInputList
if (!$numeric(vItem))
putmess "Invalid value at position %%vIndex: %%vItem"
vHasErrors = 1
endif
endfor
π― Conclusion
The forlist...endfor
loop is an indispensable tool for every Uniface developer. It provides a clean, readable solution for list processing and enables complex processing logic through its flexibility.
Try it out and let me know in the comments how you use forlist in your projects! π¬
π Based on: Uniface 10.4 Documentation (Last Updated: June 20, 2024)
π€ This post was created with AI assistance
Top comments (0)