Created
December 15, 2010 16:54
-
-
Save nathany/742242 to your computer and use it in GitHub Desktop.
Split a list into chunks, returns an array of lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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