Skip to main content
Inspiring
Jul 2, 2020 10:03 AM UTC
Answered

ArrayEach - How to stop execution?

How do you break out of the arrayEach function - stop execution?

break acts like continue

Code

<cfscript>

		myArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

		writeoutput("<h2>Loop</h2>");
		for ( counter=1; counter<=arrayLen(myArray); counter++ ) {

			thisElement = myArray[counter];
			if ( counter == 5 ) {
				continue;
			}
			if ( counter == 8 ) {
				break;
			}
			outString 	= counter & " : " & thisElement & "<br>";
			writeOutput(outString);

		};

		writeoutput("<h2>ArrayEach</h2>");
		counter = 0;
		arrayEach( myArray , function( thisElement ){

			counter += 1;
			if ( counter == 5 ) {
				continue;
			}
			if ( counter == 8 ) {
				break;
			}
			outString 	= counter & " : " & thisElement & "<br>";
			writeOutput(outString);

		});

</cfscript>

 

Output

Loop
1 : 1
2 : 2
3 : 3
4 : 4
6 : 6
7 : 7
ArrayEach
1 : 1
2 : 2
3 : 3
4 : 4
6 : 6
7 : 7
9 : 9      this should not be here

 

Answer would be much appreciated, no documentation.

This topic is closed to new replies. Start a new post to keep the conversation going.
BKBK
BKBKCorrect answer
Community Expert
Jul 11, 2020 3:06 PM UTC

Hi martiehen2,

One key consideration: we should, optimally, use language features as documented. By that I mean, as they are designed to be used and according to the official documentation. This is especially relevant in a weakly-typed language like ColdFusion. 

 

Accordingly, in ColdFusion, break;/<cfbreak> and continue;/<cfcontinue> are designed to be used in ordinary loops, not in iterative loops such as arrayEach() and structEach(). So, even if break; or continue; worked in an iterative loop, I wouldn't use it. I would think of it as suboptimal and risky, what is usually called 'undocumented'. Much like isDate('1a') or isDate('2p'), which return True.

4 replies

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
Jul 11, 2020 3:06 PM UTC

Hi martiehen2,

One key consideration: we should, optimally, use language features as documented. By that I mean, as they are designed to be used and according to the official documentation. This is especially relevant in a weakly-typed language like ColdFusion. 

 

Accordingly, in ColdFusion, break;/<cfbreak> and continue;/<cfcontinue> are designed to be used in ordinary loops, not in iterative loops such as arrayEach() and structEach(). So, even if break; or continue; worked in an iterative loop, I wouldn't use it. I would think of it as suboptimal and risky, what is usually called 'undocumented'. Much like isDate('1a') or isDate('2p'), which return True.

Translate
WolfShade
Legend
Jul 6, 2020 3:22 PM UTC

However, and I am not sure this would do what you want.. but.. since continue will 'skip' an iteration.

 

 

		writeoutput("<h2>ArrayEach</h2>");
		counter = 0;
		arrayEach( myArray , function( thisElement ){

			counter += 1;
			if ( counter == 5 ) {
				continue;
			}
			if ( counter >= 8 ) {
				continue;
			}
			outString 	= counter & " : " & thisElement & "<br>";
			writeOutput(outString);

		});

 

 

This would at least kind of, almost, act like a break in that it prevents any further processing by skipping the processing in the rest of the iterations.  Just a thought.

 

HTH,

 

^ _ ^

Translate
WolfShade
Legend
Jul 2, 2020 5:00 PM UTC

arrayEach doesn't have a break out.  Much like a forEach loop, it iterates over everything.  The only way to break out is to throw an exception.  Same for .map() or arrayMap().  Or so I understood.  Didn't think a continue would work within an arrayEach, but apparently it does.

 

If you want something that you can break out of, a for loop is probably best.

 

V/r,

 

^ _ ^

Translate
BKBK
Community Expert
Community Expert
Jul 4, 2020 3:28 PM UTC

It's exactly as WolfShade says. The each means the closure function will be executed for each element in the array.

Translate