- ColdFusion 9 Developer Tutorial
- John Farrar
- 489字
- 2021-08-05 16:16:35
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 /> #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: #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.
- iPad+Procreate數(shù)字插畫設(shè)計(jì)案例教程(全彩微課版)
- Adobe Photoshop 網(wǎng)頁設(shè)計(jì)與制作標(biāo)準(zhǔn)實(shí)訓(xùn)教程(CS5修訂版)
- Hi!扁平化Photoshop扁平化用戶界面設(shè)計(jì)教程
- Animate 2022動(dòng)畫制作:團(tuán)體操隊(duì)形
- Illustrator CS6核心應(yīng)用案例教程(全彩慕課版)
- Cinema 4D 2024+AI工具詳解與實(shí)戰(zhàn)(視頻微課·全彩版)
- 人臉識(shí)別算法與案例分析
- 斯科特·凱爾比的零基礎(chǔ)攝影后期課 Lightroom數(shù)碼照片調(diào)修技法
- Apache Roller 4.0 – Beginner's Guide
- 3ds Max 2015中文版從入門到精通
- Photoshop網(wǎng)店圖片處理實(shí)訓(xùn)教程
- Moldflow 2021模流分析從入門到精通(升級(jí)版)
- Service Oriented Java Business Integration
- 數(shù)碼攝影后期密碼Photoshop CC調(diào)色秘籍(第2版)
- Professional Azure SQL Database Administration