The usage of Classified Loops In ColdFusion

The usage of Classified Loops In ColdFusion

[ad_1]

The opposite day, I used to be staring at an educational at the Svelte JavaScript framework and I noticed that they used the $: notation to indicate reactive values. I imagine that that is simply an outdated JavaScript characteristic for labeling portions of the code (which Svelte is then overloading). I have by no means used that JavaScript characteristic myself; however, it did job my memory that some of this labeling thought may be to be had in ColdFusion as smartly, particularly for loops. I have by no means used this selection in ColdFusion both; so, I assumed it could be price a snappy exploration.

I could not to find a lot on how this selection in reality works outdoor of this article at the ColdFusion weblog. The theory is that you’ll be able to upload a label to both a for or a whilst loop; after which, eat that label in both a smash or a proceed remark. Necessarily, you’ll be able to inform ColdFusion which loop you might be referencing on your smash / proceed statements.

Which, is in point of fact most effective significant if in case you have nested loops. Differently, the smash and proceed statements merely reference the only contextual loop that you’re in.

A part of me feels that labeling loops falls below the “too artful” umbrella of programming approaches; and, is prone to make the code more difficult to learn and deal with. However, if encapsulated in the back of some significant abstraction, it may well be OK to make use of now and again.

And, to be fair, I had a troublesome time considering of a real-world situation by which I may wish to have nested loops with label-based management circulate. What I ultimately got here up with was once a fuzzy-matching set of rules for textual content seek. Given a goal price and a seek price, we wish to decide if ever letter throughout the seek price can also be positioned – so as – throughout the goal price, even though now not inside of a contiguous string of characters.

For this, I’ll have two nested loops:

  • Looping over the hunt characters.
  • Looping over the objective characters.

As I evaluate one personality to every other, I’ll have alternatives to each smash and proceed from the internal loop to the outer loop. That is arduous to give an explanation for in phrases, so let’s check out the ColdFusion code (which goes in each Adobe ColdFusion and Lucee CFML) – word that the outer loop is categorized searchLoop: and the interior loop is categorized targetLoop::

<cfscript>

	// Fuzzy fits.
	writeOutput( isFuzzyMatch( "horse", "s" ) & "<br />" );
	writeOutput( isFuzzyMatch( "horse", "hs" ) & "<br />" );
	writeOutput( isFuzzyMatch( "horse", "horse" ) & "<br />" );

	// No fits.
	writeOutput( isFuzzyMatch( "horse", "horses" ) & "<br />" );
	writeOutput( isFuzzyMatch( "horse", "take a look at" ) & "<br />" );
	writeOutput( isFuzzyMatch( "horse", "" ) & "<br />" );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I decide if the given goal textual content is a fuzzy-match for the given seek textual content.
	*/
	public boolean serve as isFuzzyMatch(
		required string targetValue,
		required string searchValue
		) {

		var searchChars = searchValue.listToArray( "" );
		var targetChars = targetValue.listToArray( "" );
		var matchFound = false;

		searchLoop:
		whilst ( searchChars.len() ) {

			var s = searchChars.shift();

			targetLoop:
			whilst ( targetChars.len() ) {

				var t = targetChars.shift();

				// We discovered an identical CHARACTER within the goal string.
				if ( s == t ) {

					if ( ! searchChars.len() ) {

						matchFound = true;
						// If we now have run out of search-characters to eat, it signifies that
						// the whole thing of the hunt key phrase was once positioned (in portions)
						// throughout the goal textual content. In different phrases, we HAVE a fuzzy-match.
						// Yay! At this pointer, there may be not anything left to look and we
						// can escape of BOTH the INNER and OUTER loops.
						smash searchLoop;

					}

					// If we nonetheless have extra seek characters to eat, transfer onto the
					// NEXT ITERATION of the OUTER loop, and the following seek personality.
					proceed searchLoop;

				}

			}

			// If we now have totally ate up the objective characters, there is no sense in
			// proceeding to eat the hunt characters - we can now not discover a event.
			smash;

		}

		go back( matchFound );

	}

</cfscript>

NOTE: On this code, there’s no want for me to label the interior loop, targetLoop: – I am simply doing it to exhibit that it can be completed.

As you’ll be able to see, each time I event a seek personality towards a goal personality, I carry out a control-flow operation that references the outer loop, searchLoop:. If I have ate up all the seek characters, I smash out of outer loop (necessarily finishing the set of rules); and, if there are extra seek characters to eat, I proceed onto to the following iteration of the outer loop. Either one of those operations are carried out from throughout the internal loop.

If we run this ColdFusion code, we get the predicted end result:

true    <!-- isFuzzyMatch( "horse", "s" ) -->
true    <!-- isFuzzyMatch( "horse", "hs" ) -->
true    <!-- isFuzzyMatch( "horse", "horse" ) -->

false   <!-- isFuzzyMatch( "horse", "horses" ) -->
false   <!-- isFuzzyMatch( "horse", "take a look at" ) -->
false   <!-- isFuzzyMatch( "horse", "" ) -->

This demo makes use of script-based loops; however, it seems that this may be to be had in tag-based loops as smartly.

I am not certain if the value-add of this system outweighs the possible cognitive price. However, it is almost certainly price having this loop labeling thought tucked away at the back of my thoughts in case I ever want it.

Need to use code from this submit?
Take a look at the license.



[ad_2]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back To Top
0
Would love your thoughts, please comment.x
()
x