coldfusion
In the past two articles, we’ve discussed some of the more complex data structures in ColdFusion. While arrays and structs are extremely useful for very specific tasks, you’ll probably find yourself using lists more often. Fortunately, lists are extremely easy to manage in ColdFusion.
在过去的两篇文章中,我们讨论了ColdFusion中一些更复杂的数据结构。 虽然数组和结构对于非常特定的任务非常有用,但是您可能会发现自己经常使用列表。 幸运的是,列表在ColdFusion中非常易于管理。
For all you Java, C++, and Visual Basic developers, I’m going to let you know now that you’ll be extremely jealous of ColdFusion after reading this article. ColdFusion makes it very easy to manipulate lists with its built-in functions. And with a little creativity, a good ColdFusion developer can surely find several million ways to make use of a list.
对于您所有的Java,C ++和Visual Basic开发人员,现在让我知道,您在阅读本文后会非常嫉妒ColdFusion。 ColdFusion通过其内置函数使操作列表变得非常容易。 优秀的ColdFusion开发人员只要有一点创造力,就能肯定找到数百万种使用列表的方法。
First, let’s define what a list is in ColdFusion terms. A list is simply a string. The only difference between a string and a list is that a list is delimited by a specific character or set of characters. You can use any string as a list, and any list can be manipulated just like any string. For example, a,b,c,d,e,f,g is a comma delimited list. Or you could use a/b/c/d/e/f/g — a forward-slash delimited list. The default delimiter for a ColdFusion list is the comma. Also, be aware that a/b/c is not the same as a / b / c. ColdFusion does not trim the white space from list elements. If a list includes spaces, you may want to use the Trim function on the elements.
首先,让我们定义ColdFusion术语中的列表。 列表只是一个字符串。 字符串和列表之间的唯一区别是列表由特定字符或一组字符定界。 您可以将任何字符串用作列表,并且可以像对待任何字符串一样操作任何列表。 例如, a,b,c,d,e,f,g是逗号分隔的列表。 或者,您可以使用a/b/c/d/e/f/g (斜杠分隔列表)。 ColdFusion列表的默认定界符是逗号。 另外,请注意, a/b/c与a / b / c 。 ColdFusion不会修剪列表元素的空白。 如果列表中包含空格,则可能要在元素上使用Trim函数。
So, now that we know what ColdFusion defines as a list, we’ll begin using lists. First of all, let’s try getting the length of a list.
因此,既然我们知道ColdFusion定义为列表的内容,我们将开始使用列表。 首先,让我们尝试获取列表的长度。
<cfset listLength = ListLen("a,b,c,d,e")> ListLen will return the value 5 to the variable listLength. That's pretty simple, but here's a challenge for you: what is the value of listLength after the following code is executed?If you guessed 5, you're right. The reason is that ColdFusion ignores empty list elements. Now, let's say that we want to display every element in the list. This is extremely easy:
<cfloop list="a,b,c,d,e" index="i"> <cfoutput> #i# <br /></cfoutput> </cfloop>This code displays each list element on a separate line. Now, if you have a list stored in a variable, don't forget to put pound signs (#) around the variable name when you pass it to the list attribute of the loop. Otherwise, it will not work correctly.
Just be sure to wrap the variable name in pound signs when you loop over a list.
There are also a few list functions that we must cover very briefly before we move on to bigger and better things. We've talked about ListLen, which returns the length of the list. ListLen takes one required parameter, which is the list you want to check the length of, and one optional parameter that specifies the delimiters you want to use. So, you can find out how many words are in a sentence by doing this:
<cfset words = ListLen("The quick brown fox jumps over the lazy dog.", " ")>Notice that the second parameter specifies that the string passed in the first parameter is a space delimited list. After execution, the words variable contains the value 9.
Another useful function is ListContains. This will search the list for a substring and return a Boolean value that indicates whether the substring was found as a list element.
Notice that this returns false because the substring "18" is not an element in the list we passed as the first parameter. Notice that we do have 186 in the list, but because there is no element that is an exact match. If you were to use the contains operator in an if statement, it would have returned true:
<cfif "12,15,186,99,0" contains "18"> this is true. </cfif>翻译自: https://www.sitepoint.com/coldfusion-iii-lists/
coldfusion