Difference between revisions of "S2 Cookbook: Logic and Flow Control"
From Dreamwidth Notes
Foxfirefey (Talk | contribs) (Created page with '== Perform actions based on conditions == == Cycle through every member of an array == == Cycle through every member of an associative array == == Group together multiple cond…') |
Foxfirefey (Talk | contribs) |
||
Line 2: | Line 2: | ||
== Cycle through every member of an array == | == Cycle through every member of an array == | ||
+ | |||
+ | <syntaxhighlight lang="s2"> | ||
+ | var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"]; | ||
+ | |||
+ | print "<ul>"; | ||
+ | foreach var string item ( $items ) { | ||
+ | print "<li>$item</li>"; | ||
+ | } | ||
+ | print "</ul>"; | ||
+ | </syntaxhighlight> | ||
== Cycle through every member of an associative array == | == Cycle through every member of an associative array == | ||
+ | |||
+ | <syntaxhighlight lang="s2"> | ||
+ | var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"}; | ||
+ | |||
+ | print "<ul>"; | ||
+ | foreach var string fruit ( $fruits ) { | ||
+ | var string color = $fruits{$fruit}; | ||
+ | print "<li>$fruit: $color</li>"; | ||
+ | } | ||
+ | print "</ul>"; | ||
+ | </syntaxhighlight> | ||
== Group together multiple conditions == | == Group together multiple conditions == | ||
[[Category: S2 Cookbook]] | [[Category: S2 Cookbook]] |
Latest revision as of 01:16, 8 August 2010
Contents
Perform actions based on conditions
Cycle through every member of an array
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"]; print "<ul>"; foreach var string item ( $items ) { print "<li>$item</li>"; } print "</ul>";
Cycle through every member of an associative array
var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"}; print "<ul>"; foreach var string fruit ( $fruits ) { var string color = $fruits{$fruit}; print "<li>$fruit: $color</li>"; } print "</ul>";