Working with associative lists in Uniface can be tricky, but the forlist/id
statement makes it a breeze! π This powerful loop construct allows you to process id=value pairs efficiently. Let me walk you through how to use it effectively.
π What is forlist/id?
The forlist/id
statement in Uniface defines a loop that processes all items in an associative list of paired items. It's perfect for handling GOLD-separated lists where each item has an identifier and a corresponding value.
π― Syntax Overview
forlist /id ItemId , ItemValue {, Index} in SourceList
Your ProcScript
endfor
Parameters breakdown:
- ItemId (String): The ID part of the list item ($idpart)
- ItemValue (String): The value part of the list item ($valuepart)
- Index (String): Item number ($itemnr) - optional parameter
- SourceList (String): GOLD-separated list to be processed
π‘ Practical Example
Let's see it in action with a real-world example processing a list of ancient cities:
variables
string vList
string vItemId
string vItemValue
string vIndex
endvariables
vList = "A=Athens;R=Rome;Sy=Syracuse;P=Pompey;Sp=Sparta"
forlist/id vItemId, vItemValue, vIndex in vList
if (vItemId = "P")
putmess "Loop processing stopped on Item number: %%vIndex, Id: %%vItemId, Value: %%vItemValue"
break
endif
putmess "Processing Item number: %%vIndex, Id: %%vItemId, Value: %%vItemValue"
endfor
π¬ Expected Output:
Processing Item number: 1, Id: A, Value: Athens
Processing Item number: 2, Id: R, Value: Rome
Processing Item number: 3, Id: Sy, Value: Syracuse
Loop processing stopped on Item number: 4, Id: P, Value: Pompey
π§ Key Features & Control Flow
The loop continues executing until one of these conditions is met:
- β All items in the list have been processed
- π’ Index exceeds the last item number
- π A
break
statement is encountered
Pro tip: πͺ You can modify the Index variable within the loop to conditionally control the iteration flow!
π Important Notes
- π Works with indexed lists too (ItemId and ItemValue will contain the same value)
- π― Can be used in all Uniface component types
- π After loop completion, Index holds the value of last item number + 1
- β‘ Perfect for processing configuration data, lookup tables, and parameter lists
π Conclusion
The forlist/id
statement is an essential tool for any Uniface developer working with associative lists. It provides clean, readable code while offering powerful control over list processing. π
This article is based on the Uniface Documentation 10.4 and was created with assistance from AI to help fellow developers understand this powerful feature better.
Happy coding! π¨βπ»π©βπ»
Top comments (0)