listEach

Iterates over every element of a List object and can call a UDF function, passed as the second argument.

listEach(String str, UDFMethod function [, String delim, boolean includeEmptyFields]);

Argument Reference for the listEach function

str

Required: Yes
An input list object.

function

Required: Yes
UDF or closure object.

delim

Required: Yes
A list delimiter to be used. The default value is comma (,).

includeEmptyFields

Required: No
Boolean. Whether to allow empty fields. Default is false.

Links more information about listEach

Examples sample code invoking the listEach function


List Loop using listEach

Using a semicolon delimiter.

list = "a;b;c";
listEach(list, function(element,index,list) {
    writeOutput("#index#:#element#;");
}, ";");

Expected Result: 1:a;2:b;3:c;


Member Function Example

List Loop list.listEach()

list = "a;b;c";
list.listEach(function(element,index,list) { 
    writeOutput("#index#:#element#;");
}, ";");

Expected Result: 1:a;2:b;3:c;


Example using a Closure

Example 1

empArray = ["john", "pete", "bob"]; 
 listS = "'john', 'pete', 'bob'"; 
 arrayEach(empArray, xclosure); 
 listEach(listS, xclosure); 
 function xclosure(empname, index) {  
    writeOutput(empName & " at index: " & index); 
 }

Expected Result: john at index: 1pete at index: 2bob at index: 3'john' at index: 1 'pete' at index: 2 'bob' at index: 3


Another Closure Example

Example 2

cityList = "Pittsburgh, Raleigh, Miami, Las Vegas"; 
 function printCity(String city) { 
    writeOutput("Current city: " & city); 
 } 
 listEach(cityList ,printCity);

Expected Result: Current city: PittsburghCurrent city: RaleighCurrent city: MiamiCurrent city: Las Vegas


Fork me on GitHub