Foreach As Loop

From Sputnik Wiki
Revision as of 20:14, 11 August 2014 by UberFoX (Talk | contribs)
Jump to: navigation, search

Contents

Foreach As

Description

Loop based on an expression.

foreach( <expression> as <value> )
{
    statements
    ...
}
foreach( <expression> as <value> from <offset> )
{
    statements
    ...
}
foreach( <expression> as <value> from <start> to <end> )
{
    statements
    ...
}

Parameters

expression

An expression to use could equal an array, string, whatever.

value

The value to place the result in most common as a $variable.

Remarks

Foreach...As statements may be nested.

If the expression contains an array the loop will cycle through each element in the array.

If the expression contains a string or numbers it will cycle through each char.

It is a bad idea to try edit the array while you are loop through it... Strange things may happen including hard crashes.

Example

Here we cycle through binary data :

$binary = Pack("ifz0", (int)100, (float)777.42, "Hello");
foreach( $binary as $item )
{
	println( 
			"Index '" . DecPad($item, 3) . "' " . 
			"Dec '$item' " .  
			"Hex '" . Hex($item) . "' " . 
			"Char '" . Chr($item) . "'"
			);
}

Here we cycle through an array :

$arr = array( 2, 4, 6, 8, 10 );
foreach( $arr as $item )
{
	println("Value is $item");
}

Here we cycle through each char in a string :

$arr = "Hello World";
foreach( $arr as $item )
{
	println("Value is $item");
}

Here we cycle through each letter in a float :

$arr = 777.42;
foreach( $arr as $item )
{
	println("Value is $item");
}

You can also referance when using arrays using the pointer & operator for example :

$arr = array( 2, 4, 6, 8, 10 );
foreach( $arr as & $item )
{
	println("Value is $item"); // Prints 2 4 6 8 10
	*$item *= 2;
	// notice the * before $item? this tells Sputnik
	// to resolve the Pointer and get the real value
	// then we can modify it
}
println("");
foreach( $arr as $item )
{
	println("Value is $item"); // Prints 4 8 12 16 20
}

See the & before $item on the first loop? This causes $item to actually LINK to the real element inside the array so changing $item will change the item in the array.

This can be seen by printing the array after changing it in the example.

Note - You can only change elements in an array! This will not work with anything else.

A reverse foreach

println("Value $i") foreach("Hello world" as $i);

Example of unpacking nested arrays with list()

$array = array(
    array(1, 2),
    array(3, 4)
);
 
foreach ($array as list($a, $b)) 
{
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b\n";
}
// Prints
// A: 1; B: 2
// A: 3; B: 4

Another example of unpacking nested arrays with list()

$array = array(
    array(1, 2, 3, 4),
    array(5, 6, 7, 8)
);
 
foreach ($array as list($a, $b, $c, $d)) 
{
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b; C: $c; D: $d\n";
}
// Prints
// A: 1; B: 2; C: 3; D: 4
// A: 5; B: 6; C: 7; D: 8

Yet another example of unpacking nested arrays with list() however this one only works if there is only 1 element in each array (key, value) and it must be a hash and not numeric (like normal arrays)

$array = array(
    array("Cat" => "Meow"),
    array("Dog" => "Woof"),
    array("Foo" => "Bar")
);
 
foreach ($array as list($key, *$value)) 
{
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "Key: $key | Value: $value\n";
}
// Prints
// Key: Cat | Value: Meow
// Key: Dog | Value: Woof
// Key: Foo | Value: Bar

Looping a .NET array

use("System");
 
$chars = %Char[]->new(3);
$chars[0] = 'A';
$chars[1] = 'B';
$chars[2] = 'C';
 
say "Printing";
foreach($chars as $c)
{
	say $c;
}
// Printing
// A
// B
// C

Looping a .NET array as a ref to modify its values in real time

use("System");
 
$chars = %Char[]->new(3);
$chars[0] = 'A';
$chars[1] = 'B';
$chars[2] = 'C';
 
say "Printing";
foreach($chars as &$c)
{
	say $c;
	*$c = 'T';
}
 
say "Printing modified array";
foreach($chars as $c)
{
	say $c;
}
// Printing
// {REF}:A
// {REF}:B
// {REF}:C
// Printing modified array
// T
// T
// T

Same as above

use("System");
 
$chars = %Char[]->new(3);
$chars[0] = 'A';
$chars[1] = 'B';
$chars[2] = 'C';
 
say "Printing";
foreach($chars as &$c)
{
	say $c;
	*$c = AscW(*$c) + 10;
}
 
say "Printing modified array";
foreach($chars as $c)
{
	say $c;
}
// Printing
// {REF}:A
// {REF}:B
// {REF}:C
// Printing modified array
// K
// L
// M

Loop through a string and print each char but do it with a ref so we can modify the string in place

$str = "Hello";
 
say "Printing string";
foreach($str as &$c)
{
	say $c;
	*$c = AscW(*$c) + 10;
}
 
say "Printing modified string";
foreach($str as $c)
{
	say $c;
}

Example of using List() with an array that contains no nested arrays

$a = array("One", "Two", "Three", "Four", "Five", "Six");
foreach($a as list($k, $j))
{
	say "$k - $j";
}
// Prints
// One - Two
// Three - Four
// Five - Six

Same as above but more arguments to extract

$a = array("One", "Two", "Three", "Four", "Five", "Six");
foreach($a as list($k, $j, $l))
{
	say "$k - $j - $l";
}
// Prints
// One - Two - Three
// Four - Five - Six

Of course unpacking only one argument is the same as a normal foreach

$a = array("One", "Two", "Three", "Four", "Five", "Six");
foreach($a as list($k))
{
	say "$k";
}
// Prints
// One
// Two
// Three
// Four
// Five
// Six

Example of using List() with a .NET array

use("System");
 
$strs = %String[]->new(8);
$strs[0] = "One";
$strs[1] = "Two";
$strs[2] = "Three";
$strs[3] = "Four";
$strs[4] = "Five";
$strs[5] = "Six";
$strs[6] = "Seven";
$strs[7] = "Eight";
 
foreach($strs as list($k, $j))
{
	say "$k - $j";
}
// Prints
// One - Two
// Three - Four
// Five - Six
// Seven - Eight
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox