Core Function Walk
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> Walk( <function>, <array> ) </pre> === Description === Walk through the arrays items and execute a user defined function on each one. === Parameters === ==== function =...") |
m (1 revision) |
||
(7 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 31: | Line 31: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
my $myarray = array(10, 20, 30, 40, 50, 60, 70); | my $myarray = array(10, 20, 30, 40, 50, 60, 70); | ||
− | $ret = Walk(function($value){ | + | $ret = Walk($myarray, function($value){++$value;}); |
println("Walk returned: $ret"); | println("Walk returned: $ret"); | ||
+ | printr($myarray); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 43: | Line 44: | ||
{ | { | ||
println("Got value $value adding to it now..."); | println("Got value $value adding to it now..."); | ||
− | + | ++$value; | |
}; | }; | ||
− | $ret = Walk($func, $myarray); | + | $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); | ||
println("Walk returned: $ret"); | println("Walk returned: $ret"); | ||
+ | printr($myarray); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
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);