Foreach As Key Value Loop

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "= Foreach As Key Value = === Description === Loop based on an expression. <pre> foreach( <expression> as <key> => <value> ) { statements ... } </pre> === Parameters =...")
 
m (1 revision)
 
(5 intermediate revisions by one user not shown)
Line 7: Line 7:
 
<pre>
 
<pre>
 
foreach( <expression> as <key> => <value> )
 
foreach( <expression> as <key> => <value> )
 +
{
 +
    statements
 +
    ...
 +
}
 +
foreach( <expression> as <key> => <value> from <offset> )
 +
{
 +
    statements
 +
    ...
 +
}
 +
foreach( <expression> as <key> => <value> from <start> to <end> )
 
{
 
{
 
     statements
 
     statements
Line 29: Line 39:
 
=== Remarks ===
 
=== Remarks ===
  
For...As..Key..Value statements may be nested.
+
Foreach...As..Key..Value statements may be nested.
 
+
If you wish to edit an array as you loop through it check out [[Core Function Each|Each( <array> )]] command see its examples one of them should tell you how to do this.
+
  
 
=== Example ===
 
=== Example ===
Line 63: Line 71:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Example of using Yield here
+
Using from
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
my $array = array("One" => "The", "Two" => "cat", "Three" => "and", "Four" => "the", "Five" => "dog");
+
$var = array(
 +
"One" => 100,
 +
"Two" => 200,
 +
"Three" => 300,
 +
"Four" => 300
 +
);
 +
 +
Foreach ($var as $key => $value from 1)
 +
{
 +
println( "Key '$key' | Value '$value'" );
 +
}
 +
</syntaxhighlight>
  
List ( $k, $v ) = Cat($array);
+
Using from and to
say "Key is: $k, value is: $v";
+
List ( $k, $v ) = Cat($array);
+
say "Key is: $k, value is: $v";
+
List ( $k, $v ) = Cat($array);
+
say "Key is: $k, value is: $v";
+
List ( $k, $v ) = Cat($array);
+
say "Key is: $k, value is: $v";
+
List ( $k, $v ) = Cat($array);
+
say "Key is: $k, value is: $v";
+
  
Function Cat( &$array )
+
<syntaxhighlight lang="sputnik">
 +
$var = array(
 +
"One" => 100,
 +
"Two" => 200,
 +
"Three" => 300,
 +
"Four" => 300
 +
);
 +
 +
Foreach ($var as $key => $value from 1 to 2)
 
{
 
{
foreach( $array as $key => $value)
+
println( "Key '$key' | Value '$value'" );
Yield return array($key, $value);
+
 
}
 
}
// Prints
 
// Key is: One, value is: The
 
// Key is: Two, value is: cat
 
// Key is: Three, value is: and
 
// Key is: Four, value is: the
 
// Key is: Five, value is: dog
 
// Yield is used to save the position in the array etc
 
// then continue from that position the next time you
 
// do a loop etc
 
// So here it saves the position each time and gives
 
// the next item in the array each time you call the
 
// function
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:37, 14 June 2015

Contents

Foreach As Key Value

Description

Loop based on an expression.

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

Parameters

expression

An expression needs to be an array for this loop to happen.

key

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

value

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

Remarks

Foreach...As..Key..Value statements may be nested.

Example

Here we cycle through binary data :

$var = array(
		"One" => 100,
		"Two" => 200,
		"Three" => 300
		);
 
Foreach ($var as $key => $value)
{
	println( "Key '$key' | Value '$value'" );
}


A reverse foreach

$var = array(
		"One" => 100,
		"Two" => 200,
		"Three" => 300
		);
 
println( "Key '$key' | Value '$value'" ) Foreach ($var as $key => $value);

Using from

$var = array(
		"One" => 100,
		"Two" => 200,
		"Three" => 300,
		"Four" => 300
		);
 
Foreach ($var as $key => $value from 1)
{
	println( "Key '$key' | Value '$value'" );
}

Using from and to

$var = array(
		"One" => 100,
		"Two" => 200,
		"Three" => 300,
		"Four" => 300
		);
 
Foreach ($var as $key => $value from 1 to 2)
{
	println( "Key '$key' | Value '$value'" );
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox