coldfusion
Structures are a very useful means of organizing data in your ColdFusion programs. But, they’re often misunderstood, overlooked, or not used to the full — even by the most skilled ColdFusion coders.
结构是在ColdFusion程序中组织数据的非常有用的方法。 但是,即使是最熟练的ColdFusion编码人员,也常常会误解,忽略或完全不理解它们。
In this article, we’ll take a look at what structures are, and how you can use them. We’ll also look at some of the most common structures that many people overlook on a regular basis.
在本文中,我们将研究什么是结构以及如何使用它们。 我们还将查看许多人经常忽略的一些最常见的结构。
First of all, what is a structure (or “struct’, as it’s known in ColdFusion)? A struct is a means of storing data. If you’ve done any programming in Java, you might liken it to a HashMap. It’s also similar to a hash in Perl. We can store a series of related data in a struct. Every piece of data has a name and a value, and we can retrieve a particular value by calling the struct and passing it the name of the value we want. Think of this process as being similar to storing several variables with different names inside one variable.
首先,什么是结构(在ColdFusion中称为“结构”)? 结构是一种存储数据的方法。 如果您已经用Java完成了任何编程,则可以将其比作HashMap。 它也类似于Perl中的哈希。 我们可以在结构中存储一系列相关数据。 每个数据都有一个名称和一个值,我们可以通过调用该结构并将其传递给所需值的名称来检索特定值。 可以认为此过程类似于在一个变量中存储几个名称不同的变量。
Let’s start with an example. Imagine we’re building an online book store and we want to store information about a single book in one data structure. Now, normally, we would probably store our data in a database, and use a query to access and display it. But, for this example, we’re going to use structs. Every book is going to have a title, an author, a description, a publish year, and an ISBN number. We’ll start by creating a struct to hold this data.
让我们从一个例子开始。 想象一下,我们正在建立一个在线书店,并且希望将有关一本书的信息存储在一个数据结构中。 现在,通常,我们可能会将数据存储在数据库中,并使用查询来访问和显示它。 但是,对于此示例,我们将使用结构。 每本书都会有标题,作者,描述,出版年份和ISBN号。 我们将从创建一个结构来保存此数据开始。
<cfset myBook = StructNew()>Here, we create a variable called myBook, and we tell ColdFusion that this is going to be a structure. If we don’t explicitly tell ColdFusion that this will be a structure, it won’t work correctly. Now, let’s add our data to the struct:
在这里,我们创建一个名为myBook的变量,然后告诉ColdFusion这将是一个结构。 如果我们没有明确告诉ColdFusion这将是一个结构,它将无法正常工作。 现在,让我们将数据添加到struct中:
<cfset a = StructInsert(myBook, \"title\", \"All About ColdFusion\", 1)>Let’s stop and talk about this StructInsert function before we move on. The first parameter of the function is the name of the struct to which we want to add a key. The next parameter is the name of the key we want to add. For this parameter, you can use a variable or a string literal (as we’ve done here). The next parameter is the value for the key. Just as with the name of the key, we can use a variable or a string literal in this case. The fourth parameter is an optional parameter — the “allow overwrite” parameter. If this parameter is set to 1, it will overwrite any existing key named “title” in this struct. If it is set to 0, an error will be thrown if you try to add “title” to a struct that already has a key by that name. The default for this parameter is 0. So, let’s add the rest of our data:
在继续之前,我们先停下来讨论这个StructInsert函数。 该函数的第一个参数是我们要向其添加键的结构的名称。 下一个参数是我们要添加的键的名称。 对于此参数,您可以使用变量或字符串文字(如我们在此处所做的那样)。 下一个参数是密钥的值。 就像键名一样,在这种情况下,我们可以使用变量或字符串文字。 第四个参数是可选参数-“允许覆盖”参数。 如果此参数设置为1 ,它将覆盖此结构中任何名为“ title”的现有键。 如果将其设置为0 ,则尝试将“ title”添加到已经具有该名称的键的结构时,将引发错误。 此参数的默认值为0 。 因此,让我们添加其余数据:
<cfset a = StructInsert(myBook, "author", "David Medlock", 1)> <cfset a = StructInsert(myBook, "description", "Information about CF", 1)> <cfset a = StructInsert(myBook, "publishyear", "2004", 1)> <cfset a = StructInsert(myBook, "ISBN", "ABCD123456", 1)>Ok, our book contains all the information we need. Now, we want to display our book. This is easy. If you know the names of all the keys for your structure, you can easily display them like this:
好的,我们的书包含了我们需要的所有信息。 现在,我们要展示我们的书。 这很简单。 如果您知道结构中所有键的名称,则可以像这样轻松显示它们:
<cfoutput> Title: #myBook["title"]#<br /> Author: #myBook["author"]#<br /> Description: #myBook["description"]#<br /> Publish Year: #myBook["publishyear"]#<br /> ISBN: #myBook["ISBN"]# </cfoutput>Here, we simply dump out the contents of the structure, one key at a time. But, what happens if we don’t know what keys a struct contains? It’s no problem — we can use ColdFusion’s loop tag to output all the keys in the struct.
在这里,我们仅转储结构的内容,一次只保留一个键。 但是,如果我们不知道结构包含哪些键,会发生什么? 没问题-我们可以使用ColdFusion的loop标记输出结构中的所有键。
<cfloop collection="#myBook#" item="key"> <cfoutput> #key#: #myBook[key]#<br /> </cfoutput> </cfloop>That’s really all we have to do in order to display all the keys in our structure and their values. But, as you’ll notice, this doesn’t output a very pretty label for each item. It just displays the key name. Let’s work on fixing that next.
为了显示结构中的所有键及其值,这实际上就是我们要做的所有事情。 但是,您会注意到,这不会为每个项目输出非常漂亮的标签。 它只显示键名。 让我们接下来修复它。
Just as we can store arrays inside arrays, we can also store structs inside structs and arrays. For example, we can create a struct called titles that will store a user-friendly label for each of the keys in our array. We’ll use the exact same keys we used in our book struct.
正如我们可以将数组存储在数组中一样,我们也可以将结构存储在结构和数组中。 例如,我们可以创建一个名为title的结构,该结构将为数组中的每个键存储一个用户友好的标签。 我们将使用与本书结构中使用的完全相同的键。
<cfset titles = StructNew()> <cfset a = StructInsert(titles, "title", "Title", 1)> <cfset a = StructInsert(titles, "author", "Author", 1)> <cfset a = StructInsert(titles, "description", "Book Description", 1)> <cfset a = StructInsert(titles, "publishyear", "Publish Year", 1)> <cfset a = StructInsert(titles, "isbn", "ISBN Number", 1)>That was easy. Now, we’ll get a little more complex with our data structures. We’re going to create a struct called bookshelf and put myBook and titles into it.
那很简单。 现在,我们将使数据结构更加复杂。 我们将创建一个称为bookshelf的结构,并将myBook和titles放入其中。
<cfset bookshelf = StructNew()> <cfset a = StructInsert(bookshelf, "names", titles, 1)> <cfset a = StructInsert(bookshelf, "values", myBook, 1)>Here, we simply initialize bookshelf as a struct and then put two keys into it. The first key is called “names“, and we put the titles struct into this key. The second key is called “values“, and we put the myBook struct into this. We now have a struct that contains two structs, each of which contains five keys. Now, I want to sort the struct by the name of the key, so I’m going to do this:
在这里,我们只是将bookshelf初始化为一个结构,然后将两个键放入其中。 第一个键称为“ names ”,我们将titles结构放入此键。 第二个键称为“ values ”,我们将myBook结构放入其中。 现在,我们有一个包含两个结构的结构,每个结构包含五个键。 现在,我想按键的名称对结构进行排序,所以我要这样做:
<cfset keyList = StructKeyList(bookshelf.names)> <cfset keyList = ListSort(keyList, "TEXT")>We’ve got a new function here, StructKeyList, which takes as a parameter the name of a structure. It then gets a list of all the keys and returns these in a comma delimited list. Then, we use the ListSort function to order the structure keys alphabetically. I do want to point out the method of accessing the structs contained in bookshelf, though. If you’ve worked with any C-style languages, you’ll recognize this as “dot notation”. You might even look at these structures as objects and see that you’re accessing their members. In this case, names is a member of bookshelf. Now, we’ll loop over our list and output the values in each of the two structs.
我们在这里有了一个新函数StructKeyList ,它以结构名作为参数。 然后,它获取所有键的列表,并以逗号分隔的列表形式返回这些键。 然后,我们使用ListSort函数按字母顺序对结构键进行排序。 不过,我确实想指出访问书架中包含的结构的方法。 如果您使用过任何C风格的语言,则将其识别为“点符号”。 您甚至可以将这些结构视为对象,并看到您正在访问它们的成员。 在这种情况下,名称是书架的成员。 现在,我们遍历列表,并输出两个结构中每个结构的值。
<cfloop list="#keyList#" index="key"> <cfoutput> #bookshelf.names[key]#: #bookshelf.values[key]#<br /> </cfoutput> </cfloop>This outputs a nice neat little listing for our book. But, we don’t often come across book stores that sell only one book. So, we’re going to need to keep track of an unlimited number of books here. In order to do this, we’ll be using our bookshelf struct, and our myBook struct; we’ll also create a new struct called myBook2 and an array called myBookArray. First, we’ll create myBook2:
这为我们的书输出了一个很好的小清单。 但是,我们很少碰到只卖一本书的书店。 因此,我们将需要在此处跟踪数量不限的图书。 为此,我们将使用bookshelf结构和myBook结构; 我们还将创建一个名为myBook2的新结构和一个名为myBookArray的数组。 首先,我们将创建myBook2 :
<cfset myBook2 = StructNew()> <cfset a = StructInsert(myBook2, "title", "More ColdFusion", 1)> <cfset a = StructInsert(myBook2, "author", "David Medlock", 1)> <cfset a = StructInsert(myBook2, "description", "More ColdFusion Info", 1)> <cfset a = StructInsert(myBook2, "publishyear", "2005", 1)> <cfset a = StructInsert(myBook2, "isbn", "321654DCBA", 1)>Now, we create a one dimensional array to store our books in.
现在,我们创建一个一维数组来存储我们的图书。
<cfset myBookArray = ArrayNew(1)>Next, we’ll add our books to the array:
接下来,我们将书籍添加到数组中:
<cfset myBookArray[1] = myBook> <cfset myBookArray[2] = myBook2>Now, we have an array containing two structs, each of which contains five keys. We’ll put these books in our bookshelf, and sort them the same way we did in the last example:
现在,我们有一个包含两个结构的数组,每个结构包含五个键。 我们将这些书放在书架中,并按照上一个示例中的方法进行排序:
<cfset a = StructInsert(bookshelf, "names", titles, 1)> <cfset a = StructInsert(bookshelf, "values", myBookArray, 1)> <cfset keyList = StructKeyList(bookshelf.names)> <cfset keyList = ListSort(keyList, "TEXT")>Now we’ll display each of our books using two loops. First, we need to make sure both books get displayed. This means we’ll be looping over the array that we’ve stored in bookshelf.values. Then, we’ll loop over the list of keys and display the label and value for each key in the current array element:
现在,我们将使用两个循环显示每本书。 首先,我们需要确保同时显示两本书。 这意味着我们将遍历存储在bookshelf.values的数组。 然后,我们将遍历键列表,并在当前数组元素中显示每个键的标签和值:
<cfloop from="1" to="#ArrayLen(bookshelf.values)#" index="i"> <cfloop list="#keyList#" index="key"> <cfoutput> #bookshelf.names[key]#: #bookshelf.values[i][key]#<br /> </cfoutput> </cfloop> <hr> </cfloop>Notice that we use the dot notation to access the array length in our outer loop. Inside that loop, we loop over the list of keys in the array. The interesting part arises when we want to display the values for the title, author, and other fields. We access the bookshelf struct, then the values key of the bookshelf struct (which is an array), then the index i of the values key (which is a struct), and, finally, we get the value of key from that struct.
注意,我们使用点表示法访问外部循环中的数组长度。 在该循环内,我们遍历数组中的键列表。 当我们要显示标题,作者和其他字段的值时,就会出现一个有趣的部分。 我们访问的书架结构,那么values的关键bookshelf结构(这是一个数组),那么指数i的的values键(这是一个结构),最后,我们得到的是结构项的值。
Are you now thoroughly confused? The first time I had to use structs at length was quite an experience. But, if you break your task down into simple steps, you’ll find that it’s actually very easy to use ColdFusion’s built-in data structures to enhance your application. Learning these elements of ColdFusion application design and programming can very quickly place you a step ahead of many other CF developers, and it can give you the tools you need to accomplish any task you’re assigned.
您现在彻底困惑了吗? 我第一次不得不冗长地使用结构是一种很丰富的经验。 但是,如果将任务分解为简单的步骤,您会发现使用ColdFusion的内置数据结构来增强应用程序实际上非常容易。 学习ColdFusion应用程序设计和编程的这些要素可以非常Swift地使您领先于许多其他CF开发人员,并且可以为您提供完成分配的任务所需的工具。
There are actually some structs that are built into ColdFusion that you probably didn’t even realize were structs. For example, did you know that the Form, URL, and CGI variable scopes are all structs? That’s right. You can easily discover what variables are in each of these structs simply by looping over the appropriate collection. (I know that all these variables appear in the ColdFusion debug information, but sometimes it’s extremely handy to know how to get this information without the aid of CF’s built-in debugging.)
实际上,ColdFusion中内置了一些结构,您甚至可能没有意识到这些结构。 例如,您知道Form,URL和CGI变量范围都是结构吗? 那就对了。 您只需遍历适当的集合,即可轻松发现每个结构中的变量。 (我知道所有这些变量都出现在ColdFusion调试信息中,但是有时在不借助CF内置调试的情况下知道如何获取此信息非常方便。)
Let’s look at a simple example with the CGI variables, which provide us with essential information about the environment our application runs in. Here’s the code to do this:
让我们看一个带有CGI变量的简单示例,它为我们提供了有关应用程序运行环境的基本信息。这是执行此操作的代码:
<cfloop collection="#CGI#" item="key"> <cfoutput>#key# = #CGI[key]#<br></cfoutput> </cfloop>That’s it! You can run this very same code to gain information on the Form, URL, CGI, Client, Application, and Session variables. These are all structures from ColdFusion’s point of view, and therefore they are easily accessible using the above method.
而已! 您可以运行相同的代码来获取有关Form,URL,CGI,Client,Application和Session变量的信息。 从ColdFusion的角度来看,这些都是结构,因此可以使用上述方法轻松访问。
That wraps up our introduction to structures in ColdFusion. As you can see, CFML provides some very powerful means of organizing, tracking, and accessing data. You may find that you don’t use these methods very often, but they can often be your saving grace when you’re in a pinch for a solution to a difficult problem. I’ve found structs and arrays to be very helpful when you have complex data that needs to be displayed in unconventional ways. Store these little tidbits of information somewhere in the back of your brain, bookmark this article in your “ColdFusion Resources” folder (I know you have one), and you’ve got one more tool in your development arsenal.
这结束了我们对ColdFusion中的结构的介绍。 如您所见,CFML提供了一些非常强大的组织,跟踪和访问数据的方法。 您可能会发现自己并不经常使用这些方法,但是当您急于解决难题时,它们通常可以作为您的省钱之选。 当您具有需要以非常规方式显示的复杂数据时,我发现结构和数组非常有用。 将这些小知识点存储在大脑的某个地方,将本文标记为“ ColdFusion资源”文件夹(我知道您有一个),并且您的开发工具库中还有另一个工具。
翻译自: https://www.sitepoint.com/coldfusion-ii-structs/
coldfusion
相关资源:asp.net知识库