DEV Community

Peter + AI
Peter + AI

Posted on

🔄 Mastering Uniface forlist/id: Loop Through Associative Lists Like a Pro

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

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

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

🔧 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)