foreach (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (→‎Usage: replaced "" with null)
 
(18 intermediate revisions by 7 users not shown)
Line 3: Line 3:
'''Introduced''': Version 1.3.b46
'''Introduced''': Version 1.3.b46


Iterates over the contents of a string list in the format "''item1, item2, item3''"
Iterates over the contents of a string list in the format {{code|"item1, item2, item3"}}.
Can also be used with JSON arrays and JSON objects.
Can also be used easily with [[Introduction_to_JSON_Datatypes|JSON arrays and JSON objects]].
[[String Property List]]s can be processed with a couple of extra functions.


====Usage====
====Usage====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[FOREACH(var, list): body]
[FOREACH(var, list): body]
[FOREACH(var, list, output_separator): body]
[FOREACH(var, list, output_separator): body]
[FOREACH(var, list, output_separator, list_separator): body]
[FOREACH(var, list, output_separator, list_separator): body]
</source>
</syntaxhighlight>


output_seperator default value is {{code|null}}<br>
{{code|output_separator}} default value is {{code|","}}<br>
list_seperator default value is ","
{{code|list_separator}} default value is {{code|","}}. Some examples of other useful separators: ''nothing'' {{code|""}}, ''space'' {{code|" "}} and ''break'' {{code|"&lt;br>"}}.


===Examples===
===Examples===
====String List Example====
====String List Example====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[FOREACH(enemy, enemyList, "<br>"): "You really hate " + enemy]
[r, FOREACH(enemy, enemyList, "<br>"): "You really hate " + enemy]
</source>
</syntaxhighlight>


====JSON Array Example====
====JSON Array Example====
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: enemyList = json.append("","Orcs, Goblins, Ogres, Trolls")]
[h: enemyList = json.append("","Orcs", "Goblins", "Ogres", "Trolls")]
[FOREACH (enemy, enemyList): "You really hate " + enemy]
[r, FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]
</source>
</syntaxhighlight>


====JSON Object Example====
====JSON Object Example====
(Note that using foreach with a JSON object will result in only the keys being returned as vars).
(Note that using foreach with a JSON object will result in only the keys being returned as vars).
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[h: enemyList = json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug"]
[h: enemyList = json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug")]
[FOREACH (enemy, enemyList): "You really hate " + enemy]
[r, FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]
</source>
</syntaxhighlight>


All the above will output:
All the above will output:
Line 42: Line 43:
  You really hate Trolls
  You really hate Trolls


====Using with [code():]====
====Using with [code():] and output_separator====


To use roll options with your FOREACH loop, you will need to use [code():] roll option.
To use roll options with your {{code|FOREACH}} loop, you will need to use {{roll|code}} roll option.  In this example I have separated the results with the string {{code|"&nbsp;then&nbsp;"}}.
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[h: enemyList="Orcs; Goblins; Ogres; Trolls"]
[h: enemyList="Orcs; Goblins; Ogres; Trolls"]
[FOREACH(enemy, enemyList, " then ", ";"), CODE:
[r, FOREACH(enemy, enemyList, " then ", ";"), CODE:
     {
     {
     [r: enemy]
     [r: enemy]
     }
     }
]
]
</source>
</syntaxhighlight>


output:
output:
  Orcs then Goblins then Ogres then Trolls
  Orcs then Goblins then Ogres then Trolls


====String Property List Example====
<syntaxhighlight lang="mtmacro" line>
[h: enemyStrProp = json.toStrProp(json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug"))]
[r, FOREACH(enemy, enemyStrProp, "<br>", ";"), code:
{
[h: enemyList = stringToList(enemy, "=")]
[h: name = listGet(enemyList, 0)]
[h: value = listDelete(enemyList, 0)]
[r: "You really hate " + name + " who are " + value]
}
]
</syntaxhighlight>
output:
You really hate Orcs who are Bob, Dave
You really hate Goblins who are Graham
You really hate Ogres who are Philip, Emanual
You really hate Trolls who are Ig, Og, Ug


===See Also===
===See Also===
[[json.append|json.append]], [[json.set|json.set]], [[code_(roll_option)|code_(roll_option)]]
{{func|json.append}}, {{func|json.set}}, {{roll|code}}, {{func|stringToList}}, {{func|listGet}}, {{func|listDelete}}


[[Category:Roll Option]]
[[Category:Roll Option]]
[[Category:Looping Roll Option]]
[[Category:Looping Roll Option]]

Latest revision as of 23:05, 22 October 2024

FOREACH Option

Introduced: Version 1.3.b46

Iterates over the contents of a string list in the format "item1, item2, item3". Can also be used easily with JSON arrays and JSON objects. String Property Lists can be processed with a couple of extra functions.

Usage

[FOREACH(var, list): body]
[FOREACH(var, list, output_separator): body]
[FOREACH(var, list, output_separator, list_separator): body]

output_separator default value is ","
list_separator default value is ",". Some examples of other useful separators: nothing "", space " " and break "<br>".

Examples

String List Example

[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[r, FOREACH(enemy, enemyList, "<br>"): "You really hate " + enemy]

JSON Array Example

[h: enemyList = json.append("","Orcs", "Goblins", "Ogres", "Trolls")]
[r, FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]

JSON Object Example

(Note that using foreach with a JSON object will result in only the keys being returned as vars).

[h: enemyList = json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug")]
[r, FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]

All the above will output:

You really hate Orcs
You really hate Goblins
You really hate Ogres
You really hate Trolls

Using with [code():] and output_separator

To use roll options with your FOREACH loop, you will need to use [code():] roll option. In this example I have separated the results with the string " then ".

[h: enemyList="Orcs; Goblins; Ogres; Trolls"]
[r, FOREACH(enemy, enemyList, " then ", ";"), CODE:
    {
    [r: enemy]
    }
]

output:

Orcs then Goblins then Ogres then Trolls

String Property List Example

[h: enemyStrProp = json.toStrProp(json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug"))]
[r, FOREACH(enemy, enemyStrProp, "<br>", ";"), code:
	{
	[h: enemyList = stringToList(enemy, "=")]
	[h: name = listGet(enemyList, 0)]
	[h: value = listDelete(enemyList, 0)]
	[r: "You really hate " + name + " who are " + value]
	}
]

output:

You really hate Orcs who are Bob, Dave
You really hate Goblins who are Graham
You really hate Ogres who are Philip, Emanual
You really hate Trolls who are Ig, Og, Ug

See Also

json.append(), json.set(), [code():], stringToList(), listGet(), listDelete()