Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. <cfscript>
  2. function listFillMissingNumbers(string list, string delim=",", string parent="", string action="skip") {
  3.  
  4. var i = "";
  5. var x = "";
  6. var missing = [];
  7. var resultList = "";
  8. var parentPos = "";
  9. var values = listToArray(arguments.list, arguments.delim);
  10.  
  11. // numbers must be in ASC order
  12. arraySort(values, "numeric");
  13.  
  14. // compare values fill in missing numbers
  15. for (i = 1; i <= arraylen(values); i++) {
  16. // skip, nothing to compare yet
  17. if (i == 1) continue;
  18.  
  19. // fill in missing numbers between PREV and NEXT number
  20. for (x = values[i-1]+1; x < values[i]; x++) {
  21. if (!arrayFind(missing, x)) {
  22. arrayAppend(missing, x);
  23. }
  24. }
  25. }
  26.  
  27. // merge numbers and build sorted list
  28. arrayAppend(values, missing, true);
  29. arraySort(values, "numeric");
  30. resultList = arrayToList(values, arguments.delim);
  31.  
  32. // remove parent from results
  33. parentPos = listFind(resultList, arguments.parent, arguments.delim);
  34. if (arguments.action eq "skip" && parentPos > 0) {
  35. resultList = listDeleteAt(resultList, parentPos, arguments.delim);
  36. }
  37.  
  38. return resultList;
  39. }
  40.  
  41. result = listFillMissingNumbers(list="1,2,4,5,7,9,10,12,14,15", parent="9", action="skip");
  42.  
  43. writeOutput("<br>result = #result#");
  44. </cfscript>
Add Comment
Please, Sign In to add comment