官术网_书友最值得收藏!

Introduction to lists and loops

Lists are stored inside string variables. You can also have a list variable stored inside a structure. Lists have a "separator" which is also known as a delimiter to divide the items so the server can evaluate the items. We are going to go back to our FAQ application concept and build on what we have learned. Let's look at the code for lists:

<!--- Example: 1_16.cfm --->
<!--- Processing --->
<cfparam name="url.speed" default="10">
<cfparam name="url.acceleration" default="0">
<cfscript>
questions = "What is the speed limit?,What is a car?,How much is gas?";
</cfscript>
<!--- Content --->
<cfoutput>
The second question is:<br />&nbsp;&nbsp;&nbsp;
#listGetAt(questions,2)#
</cfoutput>

The browser window now shows us the second question:

You can see that the list has three items in it. We have the content request the second item for display. The listGetAt() function is another one of those simple, powerful functions that makes ColdFusion so easy to program. You will find a number of wonderful list functions built into the language. We are going to mix lists and loops so you can see how things work together:

<!--- Example: 1_17.cfm --->
<!--- Processing --->
<cfparam name="url.question" default="What is the speed limit?">
<cfscript>
questions = "What is the speed limit?,What is a car?,How much is gas?";
answers = "55,Depends who you ask!,more than before";
myQuestion = listContains(questions,url.question); listContains(questions,url.question);
</cfscript>
<!--- Content --->
<cfoutput>
<strong>#listGetAt(questions,myQuestion)#</strong><br />
Answer:&nbsp; #listGetAt(answers,myQuestion)#<br /><br />
</cfoutput>
All Questions
<hr />
<cfloop list="#questions#" index="iQuestion">
<cfoutput>
<strong>Q</strong>: <a href="?question=#iQuestion#"> #iQuestion#</a><br />
</cfoutput>
</cfloop>

Our browser now shows us the power of looping in action:

We made two lists this time, one list for questions called, of course, "questions" and another for answers called "answers". When you build pairs of lists like this, check twice that they have the same number of items in each list to prevent errors. This will keep us out of the debugging phase of development. If you look where we assigned the numeric value of myQuestion, you will see that we are able to match the question asked to the list. If there is an exact match, then the number of that item in the list is returned. You should also note that in the CFLoop list, the index variable contains the actual item stored in that position in the list.

Click on different questions and see how things work. You will be able to see the variables being passed in the address bar.

Note

ListContains() will work to find exact matches in a list. If you just want to find the first item in a list with a partial match, then use ListFind(). Both of these also have a NoCase version available.

We will continue to provide more information on loops as we get into arrays in the next segment! There are several types of loops and this is one of the more popular commands among ColdFusion developers.

You are coming along great and have nearly finished your first chapter. You may not realize it yet, but by now you have started to learn to think in ColdFusion. Don't expect to be a master or even understand the fine points of applying your knowledge. That knowledge will come with the chapters that follow. Right now, we are just building a foundation.

主站蜘蛛池模板: 敦化市| 澄江县| 伊宁县| 白朗县| 剑川县| 铁岭市| 翁牛特旗| 蓬莱市| 洛阳市| 巨野县| 靖西县| 乌恰县| 红安县| 北宁市| 简阳市| 台安县| 维西| 巫溪县| 石家庄市| 河西区| 广州市| 台州市| 神农架林区| 阿拉善右旗| 泰来县| 富蕴县| 灯塔市| 湟中县| 三河市| 合作市| 正宁县| 台湾省| 连江县| 库尔勒市| 石狮市| 江门市| 易门县| 清徐县| 衡水市| 高邮市| 鄯善县|