Core Function Walk
From Sputnik Wiki
(Difference between revisions)
(→Example) |
m (1 revision) |
||
(5 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Walk( < | + | Walk( <array>, <function> ) |
</pre> | </pre> | ||
Line 8: | Line 8: | ||
=== Parameters === | === Parameters === | ||
+ | |||
+ | ==== array ==== | ||
+ | |||
+ | The array to use. | ||
==== function ==== | ==== function ==== | ||
A user defined function literal or variable containing one. | A user defined function literal or variable containing one. | ||
− | |||
− | |||
− | |||
− | |||
=== Return Value === | === Return Value === | ||
− | Success - Returns | + | Success - Returns true. |
− | Failure - Returns | + | Failure - Returns false. |
=== Remarks === | === Remarks === | ||
Line 48: | Line 48: | ||
+ | $ret = Walk($myarray, $func); | ||
+ | println("Walk returned: $ret"); | ||
+ | printr($myarray); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using both the $key and $value | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | my $myarray = array(10, 20, 30, 40, 50, 60, 70, "Test" => 80, "Foo" => 90); | ||
+ | my $func = function($key, $value) | ||
+ | { | ||
+ | println("Got Key '$key' Value '$value' adding to it now..."); | ||
+ | ++$value; | ||
+ | }; | ||
$ret = Walk($myarray, $func); | $ret = Walk($myarray, $func); | ||
println("Walk returned: $ret"); | println("Walk returned: $ret"); |
Latest revision as of 12:37, 14 June 2015
Walk( <array>, <function> )
Contents |
Description
Walk through the arrays items and execute a user defined function on each one.
Parameters
array
The array to use.
function
A user defined function literal or variable containing one.
Return Value
Success - Returns true.
Failure - Returns false.
Remarks
None.
Example
my $myarray = array(10, 20, 30, 40, 50, 60, 70); $ret = Walk($myarray, function($value){++$value;}); println("Walk returned: $ret"); printr($myarray);
Same as above
my $myarray = array(10, 20, 30, 40, 50, 60, 70); my $func = function($value) { println("Got value $value adding to it now..."); ++$value; }; $ret = Walk($myarray, $func); println("Walk returned: $ret"); printr($myarray);
Example of using both the $key and $value
my $myarray = array(10, 20, 30, 40, 50, 60, 70, "Test" => 80, "Foo" => 90); my $func = function($key, $value) { println("Got Key '$key' Value '$value' adding to it now..."); ++$value; }; $ret = Walk($myarray, $func); println("Walk returned: $ret"); printr($myarray);