foreach (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
mNo edit summary
(Updated topic to reasonable standard and to include JSONS, feel free to edit to clean up for me.)
Line 1: Line 1:
{{stub}}
===FOREACH Option===
===FOREACH Option===


Line 5: Line 4:


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 "''item1, item2, item3''"
Can also be used with JSON arrays and JSON objects.


====Usage====
====Usage====
Line 13: Line 13:
</source>
</source>


====Example====
===Examples===
====String List Example====
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
[h: enemyList="Orcs, Goblins, Ogres, Trolls"]
Line 19: Line 20:
</source>
</source>


Outputs
====JSON Array Example====
<source lang="mtmacro" line>
[h: enemyList = json.append("","Orcs, Goblins, Ogres, Trolls")]
[FOREACH (enemy, enemyList): "You really hate " + enemy]
</source>
 
====JSON Object Example====
(Note that using foreach with a JSON object will result in only the keys being returned as vars).
<source lang="mtmacro" line>
[h: enemyList = json.set("","Orcs", "Bob, Dave", "Goblins", "Graham", "Ogres", "Philip, Emanual", "Trolls", "Ig, Og, Ug"]
[FOREACH (enemy, enemyList): "You really hate " + enemy]
</source>
 
All the above will output:
  You really hate Orcs
  You really hate Orcs
  You really hate Goblins
  You really hate Goblins
  You really hate Ogres
  You really hate Ogres
  You really hate Trolls
  You really hate Trolls
====Using with [code():]====
To use roll options with your FOREACH loop, you will need to use [code():] roll option.
<source lang="mtmacro">
[h: enemyList="Orcs; Goblins; Ogres; Trolls"]
[FOREACH(enemy, enemyList, " then ", ";"), CODE:
    {
    [r: enemy]
    }
]
</source>
output:
Orcs then Goblins then Ogres then Trolls
===See Also===
[[json.append|json.append]], [[json.set|json.set]], [[code_(roll_option)|code_(roll_option)]]


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

Revision as of 04:14, 12 November 2011

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 with JSON arrays and JSON objects.

Usage

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

Examples

String List Example

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

JSON Array Example

[h: enemyList = json.append("","Orcs, Goblins, Ogres, Trolls")]
[FOREACH (enemy, enemyList): "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"]
[FOREACH (enemy, enemyList): "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():]

To use roll options with your FOREACH loop, you will need to use [code():] roll option.

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

output:

Orcs then Goblins then Ogres then Trolls


See Also

json.append, json.set, code_(roll_option)