The usage of CFLoop To Iterate Over A Document Line-By means of-Line In ColdFusion


The day prior to this, at paintings, I needed to take a CSV (Comma-Separated Values) document with 4.7 million strains of knowledge in it and damage it up into smaller recordsdata that each and every had 25K strains of knowledge. I do not do numerous document I/O (Enter, Output) at paintings, so I am a bit of rusty. I finished up the usage of the fileOpen(), fileReadLine(), and fileClose() purposes to imperatively iterate over the document with out studying it totally into reminiscence. It wasn’t till after I used to be performed that I remembered the CFLoop tag can in truth do all of that for me declaratively in ColdFusion.

I have in truth written about the usage of CFLoop to iterate over a document 13-years in the past. However, observe makes easiest; and, I hope that through exploring the subject as soon as once more I will be higher about recalling it subsequent time.

First, let us take a look at the crucial means that I used to eat the textual content document one line at a time:

<cfscript>

	echo( "<h1> The usage of FileOpen() </h1>" );

	// After we OPEN a document (versus studying a document), we permit the document to be
	// ate up as a movement. This reduces the power that we exert at the reminiscence of the
	// server.
	namesSource = fileOpen( "./names.txt", "learn", "utf-8" );
	nameIndex = 0;

	take a look at {

		// Learn one line in at a time till we hit "EOF" (Finish of Document).
		whilst ( ! fileIsEof( namesSource ) ) {

			nameIndex++;
			identify = fileReadLine( namesSource );

			echo( "#nameIndex# : #identify# <br />" );

		}

	} after all {

		fileClose( namesSource );

	}

</cfscript>

As a substitute of studying all of the document into reminiscence at one time, the fileOpen() serve as returns a document object, which can be utilized to movement knowledge from the document into reminiscence as wanted. The fileReadLine() serve as takes the document object and consumes simply sufficient persona knowledge to go back the following line. And after all, after we’re performed eating the document (after we hit the “Finish-of-Document” state), we shut the document, taking away any possible locking problems.

This code works neatly sufficient; however, we need to arrange the entire document main points on our personal. And, after we see simply how little code we finally end up having in our CFLoop resolution, even the above code turns out absurdly verbose! The next ColdFusion code does the very same factor, simplest we are the usage of a declarative means, offloading all the heavy-lifting to the CFML engine (Lucee CFML on this case):

<cfscript>

	echo( "<h1> The usage of CFLoop </h1>" );

	// As a substitute of the usage of FileOpen(), and explicitly studying strains and shutting recordsdata when
	// we are performed, we will be able to defer all the heavy-lifting to ColdFusion itself. The CFloop
	// tag encapsulates all of that good judgment!
	loop
		index = "nameIndex"
		merchandise = "identify"
		document = "./names.txt"
		charset = "utf-8"
		{

		echo( "#nameIndex# : #identify# <br />" );

	}

</cfscript>

As you’ll see, as a substitute of coping with the entire document I/O explicitly within the code, we are merely telling ColdFusion that we wish to iterate of a document, and we are letting the CFLoop tag implicitly determine how to do this maximum successfully. It is so simple!

And, after we run either one of those ColdFusion recordsdata side-by-side, we get the next output:

Top lines of a file read using both imperative fileOpen() code and declarative CFLoop code.

As you’ll see, each and every means yields the similar end result: studying the textual content document line-by-line. Alternatively, the declarative CFLoop means makes use of considerably much less code. It is ColdFusion simply superb!

Wish to use code from this publish?
Take a look at the license.



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