Lasso Soft Inc. > Home

[ array->forEach ]

Method

The [array->foreach] method invokes a tag, type, or compound expression on each element in the array. The method requires a single parameter which is a tag reference, data type instance, or compound expression. The parameter is invoked on each element of the array in turn (i.e. the tag is called with each element of the array as a parameter in turn). The parameter must return True for the method to continue. If the parameter returns False then the operation is halted.

  • Syntax
array->foreach => { expression }
Examples
  • Beginner

Perform an action on each element of an array

This example uses the [array->foreach] method to output a list of squares of the elements of an array of numbers

Code

define squareMe(n::integer=1) => {  
    return #n * #n 
}

var( an_array = array(1, 2, 3, 4))

$an_array->foreach => {^  squareMe(#1)+'  ' ^}

Result

1  4  9  16  

Recent Comments

Please add an explanation of what #1 represents. Under Lasso 8 syntax, you would call params. It seems having "loop_value" similar to iterate would have been more intuitive. To read something like: $an_array->foreach => {^ squareMe(loop_value)+' ' ^}

Posted on: 01 August 2014

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft