๐ 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
breakstatement 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
breakfor early termination when finding values -
Meaningful Variable Names:
vItemandvIndexare 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)