Core Function Walk
From Sputnik Wiki
(Difference between revisions)
(→Return Value) |
m (1 revision) |
||
(3 intermediate revisions by one user not shown) | |||
Line 19: | Line 19: | ||
=== Return Value === | === Return Value === | ||
− | Success - Returns | + | Success - Returns true. |
Failure - Returns false. | Failure - Returns false. | ||
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);