DEV Community

zuUwn21
zuUwn21

Posted on

🔄 Uniface forlist...endfor: Mastering List Loops

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

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

  1. 🔄 Iteration: Each time the loop reaches endfor, Item and Index (if defined) are incremented
  2. 🛑 Termination Conditions: The loop stops when:
    • Item number > last item number
    • Index > last item number
    • A break statement is encountered
  3. 📊 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
Enter fullscreen mode Exit fullscreen mode

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

🎯 Best Practices

✅ Do's:

  • Manipulate Index: You can change the Index within the loop for conditional control
  • Use Break: Utilize break for early termination when finding values
  • Meaningful Variable Names: vItem and vIndex are 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
Enter fullscreen mode Exit fullscreen mode

📊 Data Validation:

forlist vItem, vIndex in vInputList
    if (!$numeric(vItem))
        putmess "Invalid value at position %%vIndex: %%vItem"
        vHasErrors = 1
    endif
endfor
Enter fullscreen mode Exit fullscreen mode

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