DEV Community

Peter + AI
Peter + AI

Posted on

Mastering Uniface getitem: Your Guide to List Manipulation πŸ“‹

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
Enter fullscreen mode Exit fullscreen mode

πŸ”§ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

πŸ” 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"
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Practical Examples

Example 1: Database Selection πŸ—„οΈ

$valrep(DBMSFLD) = "mss;ora;syb;db2"
getitem vDatabase, $valrep(DBMSFLD), 2
;Result: "ora" copied to vDatabase
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

πŸ“Š 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)