Is there a better way to solve such problems?
I usually recommend working with arrays instead of lists.
In CF9, the loop is like yours, as good as it gets. You can make it a useful function if you need it more than once. This avoids StructKeyList() in order to be able to handle all types of keys, regardless of the delimiter character:
<cfscript> function GetNumericKeys(struct) { var keys = struct.keys(); var result = ArrayNew(1); var key = ""; while (keys.hasNext()) { key = keys.next(); if (IsNumeric(key)) ArrayAppend(result, key); } return result; } </cfscript>
and
<cfset nkeys = GetNumericKeys(st)>
In CF11 you can get a little more complicated (tested on CF11, I can’t say how CF10 handles this code).
<cfscript> numericIDs = arrayFilter(structKeyArray(st), function (key) { return IsNumeric(key); }); </cfscript>
To provide whole keys, use:
<cfscript> numericIDs = arrayFilter(structKeyArray(st), function (key) { return Int(key) eq key; }); </cfscript>
source share