Difference between revisions of "S2 Cookbook: Logic and Flow Control"

From Dreamwidth Notes
Jump to: navigation, search
(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…')
 
 
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

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>";

Group together multiple conditions