Break a List Down into Smaller Lists in ColdFusion

The task at hand is to take a list of 50 emails and separate them into batches of 10.

First, I’m going to make a pretend list so as not to use anyone’s real email addresses:

<cfset EmailList = "">
<cfloop from="1" to="50" index="i">
	<cfset EmailList = ListAppend(EmailList,"sample#NumberFormat(i,"00")#@email.com")>
</cfloop>

The list turns out to be:

sample01@email.com,sample02@email.com,sample03@email.com,sample04@email.com,sample05@email.com,sample06@email.com,sample07@email.com,sample08@email.com,sample09@email.com,sample10@email.com,sample11@email.com,sample12@email.com,sample13@email.com,sample14@email.com,sample15@email.com,sample16@email.com,sample17@email.com,sample18@email.com,sample19@email.com,sample20@email.com,sample21@email.com,sample22@email.com,sample23@email.com,sample24@email.com,sample25@email.com,sample26@email.com,sample27@email.com,sample28@email.com,sample29@email.com,sample30@email.com,sample31@email.com,sample32@email.com,sample33@email.com,sample34@email.com,sample35@email.com,sample36@email.com,sample37@email.com,sample38@email.com,sample39@email.com,sample40@email.com,sample41@email.com,sample42@email.com,sample43@email.com,sample44@email.com,sample45@email.com,sample46@email.com,sample47@email.com,sample48@email.com,sample49@email.com,sample50@email.com

Now then, let’s put our list an array because this post on StackOverflow suggests to do so in case of performance issues. Then we’ll loop through it, note our index and length to help us compile a given smaller list, a batch. Here is the exampe code with documentation:

<!--- Put list in an array. --->
<cfset BatchArray = ListToArray(EmailList)/>

<!--- Initialize current batch--->
<cfset CurrentBatch = "">	

<!--- Note full email list length. --->
<cfset ELLen = ListLen(EmailList)>

<!--- Loop up to length of the list.--->
<cfloop from="1" to="#ListLen(EmailList)#" index="idx">
	
	<!--- Check the list length of the current batch to be less than 10 and that the index is not on the length of the entire list. --->
	<cfif ListLen(CurrentBatch) lt 10 and idx neq ELLen>
		
		<!--- Append current email to current batch. --->
		<cfset CurrentBatch = ListAppend(CurrentBatch,BatchArray[idx])>
	
	<!--- Check if the length of the current batch is 10 or the length of the entire list. --->
	<cfelseif ListLen(CurrentBatch) eq 10 OR idx eq ELLen>
		
		<!--- Check if the index is the same as the list length. Append item. --->
		<cfif idx eq ELLen>		
			<cfset CurrentBatch = ListAppend(CurrentBatch,BatchArray[idx])>
		</cfif>		

		<!--- 
		Do what you want with the desired batch, such as storing it in a table.
		For the purposes of this example, we are simply going to output it. 
		--->
		<cfoutput>#CurrentBatch#</cfoutput><br/>
		<br/>

		<!--- Reset the batch for the next one --->
		<cfset CurrentBatch = "">
		
		<!--- 
		Add first item to the reset list. The last item in the full list
		will go here but not matter since our action is done above.  
		--->
		<cfset CurrentBatch = ListAppend(CurrentBatch,BatchArray[idx])>

	</cfif>
	
</cfloop>

Our output looks like this to show we got the list how we want it:
sample01@email.com,sample02@email.com,sample03@email.com,sample04@email.com,sample05@email.com,sample06@email.com,sample07@email.com,sample08@email.com,sample09@email.com,sample10@email.com

sample11@email.com,sample12@email.com,sample13@email.com,sample14@email.com,sample15@email.com,sample16@email.com,sample17@email.com,sample18@email.com,sample19@email.com,sample20@email.com

sample21@email.com,sample22@email.com,sample23@email.com,sample24@email.com,sample25@email.com,sample26@email.com,sample27@email.com,sample28@email.com,sample29@email.com,sample30@email.com

sample31@email.com,sample32@email.com,sample33@email.com,sample34@email.com,sample35@email.com,sample36@email.com,sample37@email.com,sample38@email.com,sample39@email.com,sample40@email.com

sample41@email.com,sample42@email.com,sample43@email.com,sample44@email.com,sample45@email.com,sample46@email.com,sample47@email.com,sample48@email.com,sample49@email.com,sample50@email.com