Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <cfscript>
- function listFillMissingNumbers(string list, string delim=",", string parent="", string action="skip") {
- var i = "";
- var x = "";
- var missing = [];
- var resultList = "";
- var parentPos = "";
- var values = listToArray(arguments.list, arguments.delim);
- // numbers must be in ASC order
- arraySort(values, "numeric");
- // compare values fill in missing numbers
- for (i = 1; i <= arraylen(values); i++) {
- // skip, nothing to compare yet
- if (i == 1) continue;
- // fill in missing numbers between PREV and NEXT number
- for (x = values[i-1]+1; x < values[i]; x++) {
- if (!arrayFind(missing, x)) {
- arrayAppend(missing, x);
- }
- }
- }
- // merge numbers and build sorted list
- arrayAppend(values, missing, true);
- arraySort(values, "numeric");
- resultList = arrayToList(values, arguments.delim);
- // remove parent from results
- parentPos = listFind(resultList, arguments.parent, arguments.delim);
- if (arguments.action eq "skip" && parentPos > 0) {
- resultList = listDeleteAt(resultList, parentPos, arguments.delim);
- }
- return resultList;
- }
- result = listFillMissingNumbers(list="1,2,4,5,7,9,10,12,14,15", parent="9", action="skip");
- writeOutput("<br>result = #result#");
- </cfscript>
Add Comment
Please, Sign In to add comment