coldfusion

tech2024-05-22  71

coldfusion

I know that this is everyone’s favorite topic. It’s certainly the first chapter I look for when I get a new Learn <Insert Language Here> in 2 Minutes book.

我知道这是每个人都喜欢的话题。 当我在2分钟内获得一本新的在此处学习<在这里插入语言>时,这肯定是我要寻找的第一章。

Well, ok — maybe not. But the gruesome truth is that data structures are at the core of our very existence as developers. Whether you’re building a shell script to run on a *nix box or a full-fledged Windows desktop application, you’ll need data structures of some type.

好吧,好吧-也许不是。 但是令人毛骨悚然的事实是,数据结构是我们作为开发人员生存的核心。 无论您是要构建要在* nix框上运行的shell脚本,还是要在成熟的Windows桌面应用程序上运行,都需要某种类型的数据结构。

Anyone who’s even briefly looked at ColdFusion has probably seen the most basic data structures, variables. Variables are generally set like this:

甚至只是简短地看过ColdFusion的任何人都可能已经看到了最基本的数据结构,即变量。 变量通常设置如下:

<cfset myVariable = \"This is my variable.\">

What many people have not experienced is the power of ColdFusion’s more advanced data structures. That’s okay, though, because in this article and the next, I’m going to introduce you to ColdFusion arrays and structures (or “structs”, as they’re called in CF).

许多人还没有体验过ColdFusion的高级数据结构的强大功能。 没关系,因为在本文和下一篇文章中,我将向您介绍ColdFusion数组和结构(或在CF中称为“结构”)。

数组 (Arrays)

We’ll begin by discussing how ColdFusion handles arrays. If you’ve developed software in any other language, you’re no doubt familiar with arrays and their various uses. If not, that’s okay — I’ll explain them here.

我们将从讨论ColdFusion如何处理数组开始。 如果您以其他任何语言开发过软件,那么您无疑会熟悉数组及其各种用途。 如果没有,那没关系-我在这里解释。

An array is simply a container that holds several variables with the same name. For example, let’s say you have a bowl of fruit and you want to create a variable for each fruit. You start with the apple:

数组只是一个包含多个具有相同名称的变量的容器。 例如,假设您有一碗水果,并且想要为每个水果创建一个变量。 您从苹果开始:

<cfset fruit = \"apple\">

Now, you’ve got a fruit that is an apple. Great! You move on to the orange:

现在,您有一个苹果水果。 大! 您转到橙色:

<cfset fruit = \"orange\">

Wait! Now you’ve got one fruit that is an orange, but no apples. This is because with the second <cfset> you overwrote “apple” with “orange”. So, how can we describe all of our fruit, but still keep it classified in the same category: fruit? Well, we use an array for this:

等待! 现在您有一个橙色的水果,但没有苹果。 这是因为在第二个<cfset>您将“ apple”改写为“ orange”。 因此,我们如何描述所有水果,但仍将其归为同一类:水果? 好吧,我们为此使用数组:

<cfset fruit = ArrayNew(1)>

This creates an array named fruit. It uses the function ArrayNew, which takes a single parameter that must be an integer. For those of you who are familiar with other programming languages (like Visual Basic, for example), this parameter is deceptive. The parameter here is not defining how many elements your array will contain, but how many dimensions it will have. Before we talk any more about dimensions, let’s continue with a single dimension array. Now that we’ve created this one, we can add our fruit to it:

这将创建一个名为fruit的数组。 它使用函数ArrayNew ,该函数接受必须为整数的单个参数。 对于熟悉其他编程语言(例如Visual Basic)的您来说,此参数具有欺骗性。 这里的参数不是定义数组将包含多少个元素,而是它将具有多少维。 在我们进一步讨论维之前,让我们继续一个维数组。 现在我们已经创建了这个,我们可以添加它的果实:

<cfset fruit[1] = \"apple\">  <cfset fruit[2] = \"orange\">  <cfset fruit[3] = \"banana\">

Perfect! Now we’ve listed our entire bowl of fruit in a single variable name, so we can easily keep track of it. Let’s examine this a little further. First of all, in ColdFusion arrays are all dynamic. This means that ColdFusion does not require you to tell it how many elements your array will have in advance (other languages, like Java, require this). Second of all, the array indexing begins at 1, not 0. Languages like Java and C++ would begin the above array by setting fruit[0] = "apple".

完善! 现在,我们用单个变量名称列出了整个水果碗,因此我们可以轻松地对其进行跟踪。 让我们进一步研究一下。 首先,在ColdFusion中,数组都是动态的。 这意味着ColdFusion不需要您事先告诉您数组将包含多少个元素(其他语言,例如Java,则需要此)。 其次,数组索引从1开始,而不是0。像Java和C ++这样的语言将通过设置fruit[0] = "apple"来开始上述数组。

Now, let’s talk about displaying an array. It’s really very simple. We’ll use a <cfloop> and the ArrayLen function.

现在,让我们谈谈显示数组。 这真的非常简单。 我们将使用<cfloop>和ArrayLen函数。

<cfloop from=\"1\" to=\"#ArrayLen(fruit)#\" index=\"i\">    <cfoutput>#i# = #fruit[i]#<br></cfoutput>  </cfloop>

That’s not so hard, is it? Those three lines of code simply dump out the contents of the array, one element per line. Notice how you access each element, using the i at the end of the variable name. What’s happening here is we’re starting our loop at 1, assigning that value to the variable i. We then display the position we’re currently at, and then access fruit[1] and display it. Next, it increments i to 2, displays the variables, and so on until i is equal to the length of the array.

那不是那么难,不是吗? 这三行代码只是转储数组的内容,每行一个元素。 注意如何使用变量名末尾的i访问每个元素。 这里发生的是我们从1开始循环,将值赋给变量i。 然后,我们显示我们当前所处的位置,然后访问fruit[1]并显示它。 接下来,它将i递增到2,显示变量,依此类推,直到i等于数组的长度。

For all you Java/C++ coders, we’re looping to the actual length of the array, not length minus one. In Java, this would throw an array out of bounds exception because indexing starts at 0 and goes to length minus one.

对于您所有的Java / C ++编码器,我们循环到数组的实际长度,而不是长度减一。 在Java中,这将引发数组超出范围的异常,因为索引从0开始,长度为减一。

多维数组 (Multidimensional Arrays)

For those of you who are unfamiliar with arrays, each value stored in an array is called an element. Each element can be almost any type of data: a variable (integer, string, Boolean, etc.), a structure, or another array. When you have an array that has an element containing another array, then you have a multi-dimensional array. Just keep in mind that if you create a multidimensional array, such as a two dimensional array, the first dimension can only contain other arrays, not actual text data (i.e. strings or integers).

对于不熟悉数组的人来说,数组中存储的每个值都称为元素。 每个元素几乎可以是任何类型的数据:变量(整数,字符串,布尔值等),结构或另一个数组。 当您拥有一个包含一个包含另一个数组的元素的数组时,您将拥有一个多维数组。 请记住,如果创建多维数组,例如二维数组,则第一维只能包含其他数组,而不能包含实际的文本数据(即字符串或整数)。

For example, let’s consider the fruit array we created above. Now, we want to keep track of the colors each fruit comes in. In order to do this, we’ll keep track of our colors in a multidimensional array. It’ll look like this:

例如,让我们考虑上面创建的水果数组。 现在,我们要跟踪每种水果的颜色。为此,我们将在多维数组中跟踪颜色。 它看起来像这样:

<cfset colors = ArrayNew(2)>  <cfset colors[1][1]  = \"green\">    <cfset colors[1][2] = \"red\">    <cfset colors[1][3] = \"yellow\">    <cfset colors[2][1] = \"orange\">    <cfset colors[3][1] = \"yellow\">    <cfset colors[3][2] = \"green\">

This may look sort of complicated, but it really isn’t. All we’re doing is:

这看起来可能很复杂,但实际上并非如此。 我们正在做的是:

defining a two dimensional array

定义二维数组 assigning values to the second dimension of each array

将值分配给每个数组的第二维

So, here’s a pop quiz: How many arrays do we have total? The answer: four. There is an array called colors that holds three other arrays, called colors[1], colors[2], and colors[3]. We treat each of the last three arrays just as we did the fruit array, assigning values to the indexes. We can now display the colors for each fruit like this:

因此,这是一个流行测验:我们总共有多少个数组? 答案:四。 有一个名为colors的数组,该数组包含其他三个数组,分别称为colors[1] , colors[2]和colors[3] 。 我们像处理水果数组一样对待后三个数组中的每个数组,将值分配给索引。 现在,我们可以像这样显示每种水果的颜色:

<cfloop from=\"1\" to=\"#ArrayLen(colors)#\" index=\"outer\">    <cfoutput><b>#outer# = #fruit[outer]#</b></cfoutput>      <blockquote>        <cfloop from=\"1\" to=\"#ArrayLen(colors[outer])#\" index=\"inner\">          <cfoutput>#inner# - #colors[outer][inner]#</cfoutput>        </cfloop>      </blockquote>    </cfloop>

Here, what we’re doing is looping over the top level array (the first dimension) and displaying the fruit name (from the fruit array). Warning: it’s not a good idea to do this. Fruit may not have as many dimensions as colors does. This is for demonstrational purposes only.

在这里,我们要做的是循环遍历顶级数组(第一个维度)并显示水果名称(来自Fruit数组)。 警告:这样做不是一个好主意。 水果可能没有颜色那么多。 这仅用于演示目的。

现实生活 (Real Life)

Unless you’re creating a very simple e-fruit stand, the above examples don’t give you much to go on when you try to use arrays in real life. Here’s an example of how I’ve use arrays on some occasions.

除非您要创建一个非常简单的电子水果摊位,否则当您尝试在现实生活中使用数组时,以上示例不会给您带来太大的帮助。 这是我在某些情况下如何使用数组的示例。

Sometimes it can get pretty tedious trying to create dropdown boxes, especially for things like the months of the year or the days of the week. Here’s a simple way to eliminate the monotony of the task. First, we’ll create and populate our array.

有时尝试创建下拉框可能会非常繁琐,尤其是对于一年中的几个月或一周中的某些天。 这是消除任务单调的简单方法。 首先,我们将创建并填充数组。

<cfparam name="selectmonth" default="1"> <cfset months = ArrayNew(1)>   <cfloop from="1" to="#DateFormat("12/31/2003", "mm")#" index="j">    <cfset months[j] = DateFormat("#j#/1/2003", "mmmm")>   </cfloop>

The above code ensures that January will be the selected month. You could use a URL or Form variable for this, depending on your application. Then, we create a loop starting at 1 and going to 12. Notice, we use the last day of the year and Put it into DateFormat() to get just the month number, or 12. We then assign each index from 1 to 12 with the name of the month. So, when we call DateFormat(“5/1/2003”, “mmmm”), it gets the date May 1st 2003 and then just pulls out the month name. Now, we’ll display our select box:

上面的代码确保一月将是选定的月份。 您可以为此使用URL或Form变量,具体取决于您的应用程序。 然后,我们创建一个从1开始到12的循环。请注意,我们使用一年的最后一天并将其放入DateFormat()以仅获取月份号或12。然后,将每个索引从1分配到12与月份的名称。 因此,当我们调用DateFormat(“ 5/1/2003”,“ mmmm”)时,它获取的日期为2003年5月1日,然后仅提取月份名称。 现在,我们将显示选择框:

<select name="month">  <cfloop from="1" to="#ArrayLen(months)#" index="i">      <cfif selectmonth eq i>        <option value="#i#" selected>#months[i]#</option>      <cfelse>        <option value="#i#">#months[i]#</option>      </cfif>    </cfloop>   </select>

This loops from the beginning index of the array to the ending index to create the drop down box. It checks to see if the current month has been selected and marks it as such if necessary. It’s that easy! You can simply copy and paste the code to create the drop down box wherever you need it.

这将从数组的开始索引循环到结束索引以创建下拉框。 它检查是否已选择了当前月份,并在必要时将其标记为当前月份。 就这么简单! 您只需在需要的地方复制并粘贴代码即可创建下拉框。

That’s it for our overview of using arrays of all sorts in ColdFusion. These handy data structures are essential in nearly every language you’ll ever encounter, so get acquainted with them very well. You never know when you might need them!

这就是我们在ColdFusion中使用各种数组的概述。 这些便捷的数据结构对于您几乎将要遇到的每种语言都是必不可少的,因此请熟悉它们。 您永远不知道何时需要它们!

In our next article, we’ll be discussing ColdFusion structures (or structs) in depth, so don’t miss it!

在下一篇文章中,我们将深入讨论ColdFusion结构(一个或多个),所以请不要错过!

翻译自: https://www.sitepoint.com/data-structures-coldfusion/

coldfusion

最新回复(0)