ListEach

Used to iterate over a delimited list and run the function closure for each item in the list.

This BIF is similar to the ArrayEach BIF, but operates on a delimited list instead of an array.

Parallel Execution

If the parallel argument is set to true, and no max_threads are sent, the filter will be executed in parallel using a ForkJoinPool with parallel streams. If max_threads is specified, it will create a new ForkJoinPool with the specified number of threads to run the filter in parallel, and destroy it after the operation is complete. Please note that this may not be the most efficient way to iterate, as it will create a new ForkJoinPool for each invocation of the BIF. You may want to consider using a shared ForkJoinPool for better performance.

Method Signature

ListEach(list=[string], callback=[function:Consumer], delimiter=[string], includeEmptyFields=[boolean], multiCharacterDelimiter=[boolean], parallel=[boolean], maxThreads=[integer], ordered=[boolean])

Arguments

Argument
Type
Required
Description
Default

list

string

true

The delimited list to perform operations on

callback

function:Consumer

true

The function to invoke for each item. The function will be passed 3 arguments: the value, the index, the array. You can alternatively pass a Java Consumer which will only receive the 1st arg.

delimiter

string

false

string the list delimiter

,

includeEmptyFields

boolean

false

boolean whether to include empty fields in the returned result

false

multiCharacterDelimiter

boolean

false

boolean whether the delimiter is multi-character

true

parallel

boolean

false

Whether to run the filter in parallel. Defaults to false. If true, the filter will be run in parallel using a ForkJoinPool.

false

maxThreads

integer

false

The maximum number of threads to use when running the filter in parallel. If not passed it will use the default number of threads for the ForkJoinPool. If parallel is false, this argument is ignored.

ordered

boolean

false

(BoxLang only) whether parallel operations should execute and maintain order

false

Examples

List Loop using listEach

Using a semicolon delimiter.

Run Example

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

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

Member Function Example

List Loop list.listEach()

Run Example

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

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

Example using a Closure

Example 1

Run Example

empArray = [ 
	"john",
	"pete",
	"bob"
];
listS = "'john', 'pete', 'bob'";
arrayEach( empArray, xclosure );
listEach( listS, xclosure );

function xclosure( Any empname, Any index ) {
	writeOutput( empName & " at index: " & index );
}

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

Run Example

cityList = "Pittsburgh, Raleigh, Miami, Las Vegas";

function printCity( String city ) {
	writeOutput( "Current city: " & city );
}
listEach( cityList, printCity );

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

Additional Examples

Run Example

list = "Plant,green,save,earth";
listEach( list, ( Any element, Any index, Any list ) => {
	writeOutput( "#index#:#element#;<br>" );
} );
// Member function with custom delimiter
writeOutput( "<br>Member Function<br>" );
strLst = "one+two+three";
strLst.listEach( ( Any element, Any index, Any list ) => {
	writeOutput( "#index#:#element#;<br>" );
}, "+" );

Last updated

Was this helpful?