Foreach As Loop

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
(Example)
 
(2 intermediate revisions by one user not shown)
Line 243: Line 243:
 
// Key: Dog | Value: Woof
 
// Key: Dog | Value: Woof
 
// Key: Foo | Value: Bar
 
// Key: Foo | Value: Bar
</syntaxhighlight>
 
 
Looping a .NET array
 
 
<syntaxhighlight lang="sputnik">
 
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
 
</syntaxhighlight>
 
 
Looping a .NET array as a ref to modify its values in real time
 
 
<syntaxhighlight lang="sputnik">
 
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
 
</syntaxhighlight>
 
 
Same as above
 
 
<syntaxhighlight lang="sputnik">
 
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
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 391: Line 306:
 
// Five
 
// Five
 
// Six
 
// Six
</syntaxhighlight>
 
 
Example of using List() with a .NET array
 
 
<syntaxhighlight lang="sputnik">
 
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
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 13:38, 29 September 2015

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

Are you trying use a Foreach on a string but getting Char data type when you want String for each char then see this example

// Define a string
$str = "Hello";
// Loop through the string as chars
// notice each one is a char data type
// this might cause problems if you expect
// to use it as a string see below for solution
foreach($str as my $c)
{
	echo vardump($c);
}
// Prints
// char'H'
// char'e'
// char'l'
// char'l'
// char'o'
 
// Solution? Cast to string
foreach($str as my $c)
{
	$c = (string)$c;
	echo vardump($c);
}
// Prints
// string(1) "H"
// string(1) "e"
// string(1) "l"
// string(1) "l"
// string(1) "o"

Using from

$a = array("Zero", "One", "Two", "Three", "Four", "Five", "Six");
foreach($a as $c from 1)
{
	say $c;
}
// Prints
// One
// Two
// Three
// Four
// Five
// Six

Using from and to

$a = array("Zero", "One", "Two", "Three", "Four", "Five", "Six");
foreach($a as $c from 1 to 3)
{
	say $c;
}
// Prints
// One
// Two
// Three

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

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