Last active
December 20, 2015 18:49
-
-
Save JamoCA/6178435 to your computer and use it in GitHub Desktop.
ColdFusion 8+ UDF to filter a list so that just numeric entries are returned. Based on http://cflib.org/udf/justNumericList Additionally filtered by number type, min/max limits & max number of values to return. Added SQL-type number translation to javacast type. Add trim to values in case user accidentally pastes tab or space characters (ie, fro…
This file contains hidden or 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> | |
| /* Based on http://www.cflib.org/udf/justNumericList | |
| Filters a list so that just numeric entries are returned. | |
| Additionally filtered by number type, min/max limits & max number of values to return | |
| Added SQL-type number translation to javacast type | |
| Add trim to values in case user accidentally pastes tab or space characters (ie, from Excel) | |
| */ | |
| function justNumericList(nList) { | |
| var intIndex = 0; | |
| var aryN = arrayNew(1); | |
| var strDelim = ","; | |
| var result = arrayNew(1); | |
| var numberType = ""; | |
| var minMaxRange = ArrayNew(1); | |
| var tempVal = ""; | |
| var goodVal = 0; | |
| var numberofGoodValues = 0; | |
| if (arrayLen(arguments) gte 2) strDelim = arguments[2]; | |
| if (arrayLen(arguments) gte 3) numberType = arguments[3]; | |
| if (arrayLen(arguments) gte 4 AND ListLen(arguments[4]) IS 2) minMaxRange = listToArray(arguments[4]); | |
| if (arrayLen(arguments) gte 5 AND VAL(arguments[5]) GT 0) numberofGoodValues = val(arguments[5]); | |
| if (numberType IS "tinyint") numberType = "byte"; | |
| else if (numberType IS "smallint") numberType = "short"; | |
| else if (numberType IS "integer") numberType = "int"; | |
| else if (numberType IS "bigint") numberType = "long"; | |
| else if (listFindNocase("float,double,decimal,real", numberType)) numberType = "bigdecimal"; | |
| aryN = listToArray(nlist,strDelim); | |
| for (intIndex=1;intIndex LTE arrayLen(aryN);intIndex=intIndex+1) { | |
| tempVal = trim(aryN[intIndex]); | |
| goodVal = 0; | |
| try { | |
| if (listfindnocase("short,int,long,float,decimal,bigdecimal", numberType)){ | |
| if (javaCast(numberType, tempVal) EQ val(tempVal)){ | |
| goodVal = 1; | |
| tempVal = javaCast(numberType, tempVal); | |
| } | |
| } else if (numberType is "boolean"){ | |
| if (ListFind("0,1", tempVal)) goodVal = 1; | |
| } else if (numberType is "byte"){ | |
| if (tempVal is int(tempVal) AND tempVal GTE 0 AND TempVal LTE 255) goodVal = 1; | |
| tempVal = int(tempVal); | |
| } else { | |
| if (not compare(val(tempVal), tempVal)){ | |
| goodVal = 1; | |
| } | |
| } | |
| if (goodVal){ | |
| if (NOT arrayLen(minMaxRange)) { | |
| ArrayAppend(result, tempVal); | |
| } else if (tempVal GTE val(minMaxRange[1]) AND tempVal LTE val(minMaxRange[2])) { | |
| ArrayAppend(result, tempVal); | |
| } | |
| if (numberofGoodValues AND ArrayLen(result) GTE numberofGoodValues){ | |
| break; | |
| } | |
| } | |
| } catch (Any e) {}; | |
| } | |
| return ArrayToList(result, strDelim); | |
| } | |
| </cfscript> | |
| <CFSET NumberList = "-245, -5, -2.346, 0, 1, 254, 12.2, 67345, 2349387345454, 123232.12323 , 100e00, 256"> | |
| <CFOUTPUT> | |
| <b>Original</b> = #NumberList# | |
| <hr><b>VAL function</b> = #justNumericList(NumberList)# | |
| <CFLOOP LIST="short,byte,tinyint,int,integer,long,bigint,float,decimal,bigdecimal,real,boolean" INDEX="numberType"> | |
| <hr> | |
| <b>#NumberType# (javacast)</b> = #justNumericList(NumberList, ",", numberType)#<br> | |
| <b>#NumberType# (-5 to 500)</b> = #justNumericList(NumberList, ",", numberType, "-5,500")#<br> | |
| <b>#NumberType# (First 4)</b> = #justNumericList(NumberList, ",", numberType, "", 4)# | |
| </CFLOOP> | |
| </CFOUTPUT> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment