| 1 |
= The foreach Statement |
| 2 |
|
| 3 |
.Syntax |
| 4 |
[source,unlang] |
| 5 |
---- |
| 6 |
foreach <attribute-reference> { |
| 7 |
[ statements ] |
| 8 |
} |
| 9 |
---- |
| 10 |
|
| 11 |
The `foreach` statement loops over a set of attributes as given by |
| 12 |
`<attribute-reference>`. The loop can be exited early by using the |
| 13 |
xref:break.adoc[break] keyword. |
| 14 |
|
| 15 |
<attribute-reference>:: |
| 16 |
|
| 17 |
The xref:attr.adoc[attribute reference] which will will be looped |
| 18 |
over. The reference can be to one attribute, to an array, a child, or |
| 19 |
be a subset. |
| 20 |
|
| 21 |
Inside of the `foreach` block, the attribute that is being looped over |
| 22 |
can be referenced as `Foreach-Variable-0`, through |
| 23 |
`Foreach-Variable-9`. The last digit is the depth of the loop, |
| 24 |
starting at "0". The loops can be nested up to eight (8) deep, though |
| 25 |
this is not recommended. |
| 26 |
|
| 27 |
The attributes being looped over cannot be modified or deleted. |
| 28 |
|
| 29 |
.Example |
| 30 |
[source,unlang] |
| 31 |
---- |
| 32 |
foreach &Class { |
| 33 |
update reply { |
| 34 |
Reply-Message += "Contains %{Foreach-Variable-0}" |
| 35 |
} |
| 36 |
} |
| 37 |
---- |
| 38 |
|
| 39 |
// Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 40 |
// Development of this documentation was sponsored by Network RADIUS SAS. |