ColdFusion 循环遍历列表的[仅部分]

发布于 2024-11-29 08:37:49 字数 148 浏览 0 评论 0原文

真的很难找到一种方法来循环冷融合列表的一部分(或者一半?)。我设置了一个 if 语句来检查列表的长度以及是否超过 30...我想将列表拆分为前 30 个和其余部分?但不确定这是否是最好的解决方案。我真的不需要太多细节,我相信我自己能弄清楚这么多,我更希望被指出正确的方向......

Really struggling with finding a way to loop through only part (or half maybe?) of a coldfusion list. I've got an if statement 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯了 2024-12-06 08:37:49

不要循环列表,而是从 1 循环到数字,并在循环中使用 listGetAt()。对于列表的其余部分,只需从#myvar + 1# 循环到#listLen#。

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

当然,这不是最有效的方法。如果遇到性能问题,可能需要通过 listToArray() 将列表转换为数组,然后执行以下操作:

<cfset myArray = listToArray( myList ) />

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

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>
沐歌 2024-12-06 08:37:49

您可以利用底层的 java 函数。

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

将为您提供一个数组“testChunk”,其中包含列表中的前 30 个项目。您现在可以轻松地循环数组的元素。

为了更清楚地说明这一点,下面是一个示例:

<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>

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>
安稳善良 2024-12-06 08:37:49

这实际上取决于您想要完成的任务、如何使用这两个独立的数据集以及列表中的数据类型......数据真的需要分离吗?你可以吗:

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

如果你不需要采取额外的步骤来分成两个列表或数组,那么就可以节省一些编码和工作量。执行时间

另外 - 如果您的起点发生变化,您始终可以:

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

-sean

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

度的依靠╰つ 2024-12-06 08:37:49

在 CF10 或 Railo 4 中,您可以使用 first()rest() 函数来自 Underscore.cfc 来拆分您的列表:

_ = new Underscore();

myArray = listToArray(myList);

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

_.first()_.rest() 都根据传递给它们的索引返回新数组。这些方法只是委托给本机 arraySlice() 函数,但它们可以帮助您编写更具表现力的代码。

注:我写的是Underscore.cfc

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文