CFLib.org – Common Function Library Project

ListContainsConsecutiveInt(lsIncoming[, strDelimiter])

Last updated May 9, 2003

author

Teri Stewart

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Returns true if a list consists of integers that are consecutive, false if it does not. The minimum number in the list has no bearing; it may be any integer, negative or positive.

Return Values:
Returns a boolean.

Example:

<cfoutput>
<!--- consecutive but unordered --->
#ListContainsConsecutiveInt("9,10,12,11")#<br>
<!--- consecutive with pipe delimiter --->
#ListContainsConsecutiveInt("-1|0|1","|")#<br>
<!--- consecutive, but repeated number --->
#ListContainsConsecutiveInt("4,4,5")#<br>
<!--- not consecutive --->
#ListContainsConsecutiveInt("6,4")#<br>
<!--- consecutive but not integers --->
#ListContainsConsecutiveInt("2.5,3.5")#<br>
<!--- alpha --->
#ListContainsConsecutiveInt("1,2,a")#<br>
</cfoutput>

Parameters:

Name Description Required
lsIncoming List to check. Yes
strDelimiter Delimiter for the list. Defaults to a comma. No

Full UDF Source:

/**
 * Determines if a list consists of consecutive integers, regardless or order.
 * 
 * @param lsIncoming 	 List to check. (Required)
 * @param strDelimiter 	 Delimiter for the list. Defaults to a comma. (Optional)
 * @return Returns a boolean. 
 * @author Teri Stewart (terilynnstewart@hotmail.com) 
 * @version 1, May 9, 2003 
 */
function ListContainsConsecutiveInt(lsIncoming){
  var arrSorted=ArrayNew(1);
  var i=0;
  var strDelimiter=",";

 //default list delimiter to a comma unless otherwise specified
 if(ArrayLen(arguments) gte 2){
  strDelimiter=arguments[2];
 }
  //change list to array for faster processing
  arrSorted=ListToArray(lsIncoming,strDelimiter);
  //make sure all array elements are numeric and itegers
  for(i=1; i lte ArrayLen(arrSorted); i=i+1){
    if(not isNumeric(arrSorted[i]) or round(arrSorted[i]) is not arrSorted[i]){
      return false;
    }
  }
  //sort the array
  arraySort(arrSorted,"numeric");
  //loop sorted array
  for(i=0; i lt ArrayLen(arrSorted); i=i+1){
    //see if item at array position i+1 equals the first array element + i
    if(arrSorted[i+1] neq arrSorted[1]+i){
      return false;
      break;
    }
  }
  return true;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson