Difference between revisions of "S2 Cookbook: Arrays"

From Dreamwidth Notes
Jump to: navigation, search
(Created page with '== Access an item of an array or associative array == == Cycle through all the items of an array or associative array == == Get the last item of an array == [[Category: S2 Coo…')
 
m (Get a random array item)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Access an item of an array or associative array ==
 
== Access an item of an array or associative array ==
 +
 +
<syntaxhighlight lang="s2">
 +
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 +
var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"};
 +
 +
## Accessing an array
 +
 +
# sock
 +
print $items[0];
 +
# pants
 +
print $items[1];
 +
 +
## Accessing an associative array
 +
 +
# red
 +
print $fruits{"apple"};
 +
# yellow
 +
print $fruits{"lemon"};
 +
# purple
 +
print $fruits{"grape"};
 +
</syntaxhighlight>
  
 
== Cycle through all the items of an array or associative array ==
 
== Cycle through all the items of an array or associative array ==
 +
 +
Please see the recipes:
 +
 +
* [[S2_Cookbook:_Logic_and_Flow_Control#Cycle_through_every_member_of_an_array|Cycle through every member of an array]]
 +
* [[S2_Cookbook:_Logic_and_Flow_Control#Cycle_through_every_member_of_an_associative_array|Cycle through every member of an associative array]]
 +
 +
== Get the number of items in an array or associative array ==
 +
 +
Use the <code>size</code> keyword:
 +
 +
<syntaxhighlight lang="s2">
 +
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 +
var int items_length = size $items;
 +
 +
var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"};
 +
var int fruits_length = size $fruits;
 +
 +
# Items: 5; Fruits: 3
 +
print "Items: $items_length; Fruits: $fruits_length";
 +
</syntaxhighlight>
  
 
== Get the last item of an array ==
 
== Get the last item of an array ==
 +
 +
When you use a negative number as the index to an array, the count starts from the end of the array:
 +
 +
<syntaxhighlight lang="s2">
 +
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 +
# scarf
 +
print $items[-1];
 +
# skirt
 +
print $items[-2];
 +
</syntaxhighlight>
 +
 +
== Get a random array item ==
 +
 +
Use the <code>rand</code> function to get an index within the array's range:
 +
 +
<syntaxhighlight lang="s2">
 +
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 +
# goes from 0 to size-1 in indexing
 +
var int random = rand(0, size $items - 1 );
 +
var string pick = $items[$random];
 +
print "Pick: $pick (item $random)";
 +
</syntaxhighlight>
 +
 +
You can even do it entirely inline:
 +
 +
<syntaxhighlight lang="s2">
 +
var string pick = $items[rand(0, size $items - 1 )];
 +
</syntaxhighlight>
 +
 +
== Reverse an array ==
 +
 +
Use the <code>reverse</code> keyword to reverse an array:
 +
 +
<syntaxhighlight lang="s2">
 +
var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 +
 +
# scarf, skirt, shirt, pants, sock,
 +
foreach var string item ( reverse $items ) {
 +
    print "$item, ";
 +
}</syntaxhighlight>
  
 
[[Category: S2 Cookbook]]
 
[[Category: S2 Cookbook]]

Latest revision as of 19:03, 10 August 2010

Access an item of an array or associative array

var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"};
 
## Accessing an array

# sock
print $items[0];
# pants
print $items[1];
 
## Accessing an associative array

# red
print $fruits{"apple"};
# yellow
print $fruits{"lemon"};
# purple
print $fruits{"grape"};

Cycle through all the items of an array or associative array

Please see the recipes:

Get the number of items in an array or associative array

Use the size keyword:

var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
var int items_length = size $items;
 
var string{} fruits = {"apple" => "red", "lemon" => "yellow", "grape" => "purple"};
var int fruits_length = size $fruits;
 
# Items: 5; Fruits: 3
print "Items: $items_length; Fruits: $fruits_length";

Get the last item of an array

When you use a negative number as the index to an array, the count starts from the end of the array:

var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
# scarf
print $items[-1];
# skirt
print $items[-2];

Get a random array item

Use the rand function to get an index within the array's range:

var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
# goes from 0 to size-1 in indexing
var int random = rand(0, size $items - 1 );
var string pick = $items[$random];
print "Pick: $pick (item $random)";

You can even do it entirely inline:

var string pick = $items[rand(0, size $items - 1 )];

Reverse an array

Use the reverse keyword to reverse an array:

var string[] items = ["sock", "pants", "shirt", "skirt", "scarf"];
 
# scarf, skirt, shirt, pants, sock, 
foreach var string item ( reverse $items ) {
    print "$item, ";
}