Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Split a list into chunks, returns an array of lists
<cfscript>
function ListSplit(list, chunk_size, delim)
{
var result = ArrayNew(1); var re = ""; var start = 1;
while(1) {
re = REFind("((?:[^#delim#]+#delim#){1,#chunk_size#})", list & delim, start, "true");
if( re.len[1] eq 0 ) break;
ArrayAppend(result, Mid(list,re.pos[1],re.len[1]-len(delim)));
start = re.pos[1] + re.len[1];
if(start gte len(list)) break;
}
return result;
}
</cfscript>
<cfscript>
function ListSplit(list,chunk_size,delim) {
var result = ArrayNew(1); var el = ''; var i = 0;
var chunk = 0;
for(i = 0; i lt ListLen(list); i = i + 1) {
el = ListGetAt(list, i+1, delim);
if(i mod chunk_size eq 0) { chunk = chunk + 1; result[chunk] = ''; }
result[chunk] = ListAppend(result[chunk], el, delim); // perhaps not the fastest?
}
return result;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.