Lasso Soft Inc. > Home

[ Iterate ]

Method

The [Iterate] method loops through each element of a data type, such as an array or a map. A variable is set to the value of each element of the data type in turn. This allows the same operation to be performed on each element.

The first parameter to the [Iterate] method should be the data type through which iteration should be performed. The second parameter should be a reference to a variable that will be set to each item in turn. The second parameter is usually specified as local(VariableName).

The [Iterate] method can be used on any data type that supports the [...->Get] and [...->Size] methods including maps, pairs, arrays, and strings. Custom data types are also supported providing they support the required methods.

Note: The [Iterate] method can be used rather than using [Loop] on the size of an array and then [Array->Get] to fetch each particular item.

  • Syntax
iterate(Data Type, local(Variable Name))
	code
/iterate

iterate(Data Type, local(Variable Name)) => {
	code
}
Examples
  • Beginner

To iterate through an array:

Use the [Iterate] method. In the following example each element of an array is output on a separate line.

Code

local(a = array('Rhino','Kiwi','Platypus'))
iterate(#a, local(i))
	#i
	'<br>'
/iterate

Result

Rhino
Kiwi
Platypus

To access the current iteration value:

Use [loop_value] within the [iterate] method.

Code

local( x = 0 )

iterate( array(1,2,3) ) => {
  #x += loop_value
}

#x

Result

6

Recent Comments

No Comments found

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