Foreach As Loop

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 274: Line 274:
 
say $c;
 
say $c;
 
}
 
}
 +
</syntaxhighlight>
 +
 +
Example of using List() with an array that contains no nested arrays
 +
 +
<syntaxhighlight lang="sputnik">
 +
$a = array("One", "Two", "Three", "Four", "Five", "Six");
 +
foreach($a as list($k, $j))
 +
{
 +
say "$k - $j";
 +
}
 +
// Prints
 +
// One - Two
 +
// Three - Four
 +
// Five - Six
 +
</syntaxhighlight>
 +
 +
Same as above but more arguments to extract
 +
 +
<syntaxhighlight lang="sputnik">
 +
$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
 +
</syntaxhighlight>
 +
 +
Of course unpacking only one argument is the same as a normal foreach
 +
 +
<syntaxhighlight lang="sputnik">
 +
$a = array("One", "Two", "Three", "Four", "Five", "Six");
 +
foreach($a as list($k))
 +
{
 +
say "$k";
 +
}
 +
// Prints
 +
// One
 +
// Two
 +
// Three
 +
// Four
 +
// Five
 +
// Six
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 10:02, 9 August 2014

Contents

Foreach As

Description

Loop based on an expression.

foreach( <expression> as <value> )
{
    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

For...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.

If you wish to edit an array as you loop through it check out Each( <array> ) command see its examples one of them should tell you how to do this.

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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox