# foreach\_ext

Wendet eine Function `f()` auf alle Elemente in einer Tabelle `t` an. Es können entsprechende zusätzliche Parameter mit übergeben werden für `f()`&#x20;

```lua
function foreach_ext (t, f, ...)
    local i, v = next(t, nil)
    while i do
      local args = arg
      tinsert(args,1,v)
      tinsert(args,1,i)
      
      local res = call(f,args)
      
      tremove(args,1); -- it is the same object hence remove it again
      tremove(args,1);
      
      if res then return res end
      i, v = next(t, i)
    end
end
```

Beispiel mit Parameterübergabe:

```lua
t = { 10,20,30 }
foreach_ext(t,function(i,v,msg) print(msg .. " " .. i .. " " ..v) end,"hello")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.settlers-united.com/s4-lua-api-de/tutorials/code-snippets/foreach_ext.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
