开发者

ColdFusion looping through [only part] of a list

Really struggling with finding a way to loop through only part (or half maybe?) of a coldfusion list. I've got an if s开发者_如何学运维tatement set up to check the length of the list and if it is over 30... I want to split the list into the first 30 and the remainder? Not sure if that's the best solution though. I really don't need much detail I'm sure I can figure that much out myself I am more looking just to be pointed in the right direction...


Rather than looping over the list, loop from 1 to a number, and use listGetAt() in the loop. For the remainder of the list, just loop from #myvar + 1# to #listLen#.

<cfoutput>
    <cfloop from="1" to="#myVar#" index="idx">
        #listGetAt( myList, idx )#<br />
    </cfloop>
</cfoutput>

Granted, it's not the most efficient method. If you encounter performance issues, might want to convert the list to an array via listToArray(), and then do:

<cfset myArray = listToArray( myList ) />

<cfoutput>
    <cfloop from="1" to="#myVar#" index="idx">
        #myArray[ idx ]#<br />
    </cfloop>
</cfoutput>


You can make use of the underlying java functions.

<cfscript>
testList = "1,2,3,4,5,6,7,8,9,10,...,43,44";
listAsArray = listToArray(testList);
testChunk = listAsArray.subList(0,30);
</cfscript>

will give you an array "testChunk" with the first 30 items in the list. You can now easily loop over the elements of the Array.

To make this more clear, here is an example:

<cfscript>
    testList = "";
    maxChunkLength = 30;
    for (i=1;i lte 100; i=i+1){
        testList = listAppend(testList, i);
    }
    numOfChunks = ceiling(listLen(testList)/maxChunkLength);
    listAsArray = listToArray(testList);
    numOfItems = arraylen(listAsArray);
    for (k=1;k lte numOfChunks; k=k+1){
        startItem = (k - 1) * maxChunkLength;
        endItem = startItem + maxChunkLength;
        if (endItem gt numOfItems){
            endItem = numOfItems;
        } 
        writeOutput(listAsArray.subList(startItem, endItem).toString() & "<br />");
    }
</cfscript>


it really depends on what you are trying to accomplish, how the 2 separated sets of data will be used and whst type of data you have in the list... does the data really need to be separated? can you just:

<cfloop from="31" to="#listLen(myList)#" index="i">
      #listGetAt(myList, i)#
</cfloop>

If you don't need to take the extra step of separating into 2 lists or arrays, save your self some coding & execution time

Also - if your starting point changes, you can always:

<cfloop from="#start#" to="#listLen(myList)#" index="i">
     #listGetAt(myList, i)#
</cfloop>

-sean


In CF10 or Railo 4, you could use the first() and rest() functions from Underscore.cfc to split up your list:

_ = new Underscore();

myArray = listToArray(myList);

firstThirty = _.first(myArray, 30);
remaining = _.rest(myArray, 31);

_.first() and _.rest() both return new arrays based on the index passed into them. The methods simply delegate to the native arraySlice() function, but they can help you write more expressive code.

Note: I wrote Underscore.cfc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜