foreach (roll option): Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (→‎Using with [code():]: Updated to include that I've used output_seperator)
m (→‎Examples: forgot to add <br> to output_seperator in examples.)
Line 26: Line 26:
<source lang="mtmacro" line>
<source 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]
[FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]
</source>
</source>


Line 33: Line 33:
<source lang="mtmacro" line>
<source 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]
[FOREACH (enemy, enemyList, "<br>"): "You really hate " + enemy]
</source>
</source>



Revision as of 09:47, 13 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]

output_seperator default value is null
list_seperator default value is ","

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, "<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"]
[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_seperator

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"]
[FOREACH(enemy, enemyList, " then ", ";"), CODE:
    {
    [r: enemy]
    }
]

output:

Orcs then Goblins then Ogres then Trolls

See Also

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