Working with lists in Uniface? The getitem
statement is your best friend for extracting data from both indexed and associative lists. This comprehensive guide will walk you through everything you need to know about this powerful feature. Note: This article was crafted with AI assistance and is based on the official Uniface 10.4 documentation.
π― What is getitem?
The getitem
statement copies an item from a list to a field or variable. Whether you're dealing with simple indexed lists or complex associative lists with ID=value pairs, getitem
has you covered.
π Basic Syntax
There are two main forms of the getitem
statement:
getitem Target, IndexedList, Index
getitem/id{/case} Target, AssociativeList, ItemId
π§ Qualifiers
- /id: Get the item specified by Index from an associative list
- /case: Both the value and case of Index must match the item in the list
π’ Working with Indexed Lists
For indexed lists, simply specify the position of the item you want to extract:
$valrep(DBMSFLD) = "mss;ora;syb;db2"
getitem vItem, $valrep(DBMSFLD), 3
;Result: "syb" copied to vItem
Pro tip: Use -1
as the index to get the last item in the list! π
π·οΈ Working with Associative Lists
Associative lists contain ID=value pairs, making them perfect for key-value scenarios:
$valrep(DATEFLD) = "mon=monday;tue=tuesday;wed=wednesday;weekend=sat!;sun"
getitem/id vDay, $valrep(DATEFLD), "TUE"
;Result: "tuesday" copied to vDay
π Case Sensitivity
By default, matching is case-insensitive. Add the /case
qualifier for exact matching:
getitem/id/case vItem, vList, "ab" ; Only matches exactly "ab"
π‘ Practical Examples
Example 1: Database Selection ποΈ
$valrep(DBMSFLD) = "mss;ora;syb;db2"
getitem vDatabase, $valrep(DBMSFLD), 2
;Result: "ora" copied to vDatabase
Example 2: Weekend Planning π
$valrep(DATEFLD) = "mon=monday;tue=tuesday;wed=wednesday;weekend=sat!;sun"
getitem/id vWeekend, $valrep(DATEFLD), "weekend" ;"sat;sun" copied to vWeekend
getitem vDay, vWeekend, 1 ;"sat" copied to vDay
Example 3: Treating Associative as Indexed π
$valrep(DATEFLD) = "mon=monday;tue=tuesday;wed=wednesday;weekend=sat!;sun"
getitem vDay, $valrep(DATEFLD), 3
;Result: "wed=wednesday" copied to vDay (entire ID=value pair)
π Return Values
The getitem
statement returns useful status information in $status
:
- 0: No item was copied; Target is empty
- >0: Sequence number of the list item that was copied
πͺ Best Practices
- Always check
$status
to verify successful operations β - Use
/case
when exact matching is required π― - Remember that indexed access on associative lists returns the full ID=value pair π
- Consider using
-1
as index for the last item instead of calculating list length π
π Conclusion
The getitem
statement is an essential tool in any Uniface developer's toolkit. Whether you're working with simple indexed lists or complex associative structures, mastering getitem
will make your data manipulation tasks much more efficient and elegant.
Happy coding! π»β¨
Top comments (0)