120
©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is not the eternal Name. —Lao-Tzu

©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

Embed Size (px)

Citation preview

Page 1: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

©2003 Hal Helms, Inc.

Variables

The Tao that can be told is not the eternal Tao.The name that can be named is not the eternal Name. —Lao-Tzu

Page 2: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 2

Data types

Variables have a property called a data type that specifies the type of data that a variable holds– integer– string– decimal number– boolean value– record set

Page 3: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 3

Strong typing

Some languages require that you declare a variable's data type prior to using it– String state = "Indiana"; // Java

Once declared, a variable's data type cannot change– boolean state = true; // illegal!!

– Such languages are called strongly-typed

Page 4: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 4

Strong typing: pros & cons

Pros– allows compiler (most strongly-typed languages are also

compiled languages) to optimize performance

– enforced rigor helpful when dealing with large-scale or complex applications

Cons– slower development

Page 5: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 5

Weak typing

Other languages do not require that you declare a variable's data type prior to using it<cfset state = "Minnesota"> <!---CF--->

Data types can change<cfset state = "Minnesota"> <!---OK---><cfset state = true> <!---OK---><cfset state = 26> <!---OK--->

Such languages are called weakly-typed

Page 6: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 6

Weak typing: pros and cons

Pros– no compile cycle (most weakly typed languages are

interpreted languages)

Cons– easier to make mistakes in overwriting variables– slower execution since all data typing must be done at run

time– harder to maintain code

Page 7: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 7

Compiled languages

Some languages have a separate compile cycle during which several checks are made including checking variables are for valid types, correct naming and scoping of variables, etc.– Java– C#

After the compile cycle is completed, the program can be run

When the program runs, we are said to be in run time

Page 8: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 8

Interpreted languages

Some languages have no separate compile cycle but instead do all checking at run time– ColdFusion– Smalltalk

Page 9: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 9

ColdFusion?

ColdFusion is…– weakly-typed– interpreted

Page 10: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 10

Data types can be simple…

String‘Welcome to Programming Foundations’

Numeric25 50.75

Date{ts '2001-03-30 17:45:43'}

BooleanTRUE FALSE

Page 11: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 11

…or complex

Structure– collection of name/value pairs

Array– numbered collection of other data types (not name/value pairs)

Query result set– return by a <cfquery>

List– Pat, Wesley, Donna, Kathy, Michael, Jim

Page 12: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 12

More on variables

Variables have two important properties– scope: where the variable can be accessed from– lifespan (or span): how long the variable lives

Page 13: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 13

Scopes in ColdFusion

Variables that can be seen by the page that called them as well (including <cfinclude>d files) are called local variables

Variables that can be seen by all pages (including custom tags and <cfmodule>d files) are called global variables

Page 14: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 14

Lifespans in ColdFusion

Most variables live only for a single page request Those that live across multiple

page requests are called persistent

Client

Web server

ColdFusion serverDatabase server

Page 15: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 15

Variable matrix

Scope Spanvariables local page request

request global page requestform global page requestURL global page request

attributes local page requestcaller local page request

CGI global page requestcookie global persistentsession global persistentclient global persistentapplication global persistentserver global persistent

Page 16: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 16

Creating/setting a variable

Method 1 (common)<cfset foo = "bar">

Method 2<cfset SetVariable('foo', 'bar')>

Page 17: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 17

Local scope examples

<cfset thisClass = “ColdFusion MX Foundations”>

<cfset SetVariable(‘isReady’,‘TRUE’)>

<cfset variables.myName = “Hal”>

<cfset myName = "Hal">*

*Unscoped variables are part of the default “variables” local scope

Page 18: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 18

Specifying different scopes

<cfset variables.foo = "bar"><cfset foo = "bar"><cfset session.foo = "bar"><cfset request.foo = "bar"><cfset application.foo = "bar"><cfset server.foo = "bar"><cfset caller.foo = "bar"><cfset SetVariable('client.foo', 'bar')>

same

Page 19: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 19

Form variables

When a form is submitted through the post method, any form fields are placed into the form scope of the form processor (the page set in the form's action property) and can be accessed by that page

<form action="MyPage.cfm" method="post"><input

type="hidden" name="today" value="Monday">

…</form>

Page 20: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 20

URL variables

You can pass variables on a URL in a query string The query string is created by appending a ? to

the URL and then adding name=value pairs separated by an ampersand

<a href="MyPage.cfm?userID=5&permissions=12">

These variables are then accessible to the called page in the URL scope

Page 21: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 21

Displaying variables

Wrap pound signs around a variable and set within <cfoutput> tags<cfoutput>

Hello, there, #yourName#</cfoutput>

You don't need to do this within CF tags and functions (usually)<cfset shipping = “5.00”><cfset total = subTotal + shipping><cfset ArrayAppend(arr, 42)>

Page 22: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 22

The Evaluate() function

Use it when you want to force an evaluation of a name as a variable.

Example:– When you submit a form to a processing page, you get a

variable called form.fieldlist, a list of the form fields…

Page 23: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 23

Evaluate()

<cfloop list=“#form.fieldList#” index=“aFormField”><cfoutput>

#aFormField#</cfoutput>

</cfloop>

What will this produce?

Page 24: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 24

Evaluate()

<cfloop list=“#form.fieldList#”index=“aFormField”><cfoutput>

#Evaluate(‘form.’ & aFormField)#</cfoutput>

</cfloop>

What will this produce?

Page 25: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

©2003 Hal Helms, Inc.

Complex Variables

A child of five would understand this.

Send someone to fetch a child of five. —Groucho Marx

Page 26: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 26

Complex data types in ColdFusion

Structure– collection of name/value pairs

Array– numbered collection of data

Query result set– return by a <cfquery>

List– series of tokens and delimiters

Bob,Sue,Frank,Anne 21|257|TRUE|c:\cfusionmx\wwwroot\test

Page 27: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 27

What is a structure?

Set of name/value pairs that all relate to a central topic or idea

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Page 28: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 28

Why structures?

Organizes related data into a single variable that is simpler to store and manipulate

Provides data encapsulation

Page 29: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 29

Working with structures

1. aStructure.author John Irving

valuestructure-name

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

2. aStructure[‘author’] John Irving

keys values

key

Page 30: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 30

Structure limitations

Structures can hold as values…– simple values– arrays– structures– result sets– lists

Page 31: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 31

Create a structure

Method 1<cfset aBook = StructNew()><cfset aBook.ISBN = “287898988”><cfset aBook.title = “A Prayer for Owen Meany”><cfset aBook.author = “John Irving”><cfset aBook.publisher = “Ballantine Books”>

Page 32: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 32

Create a structure

Method 2<cfset aBook = StructNew()><cfset aBook[‘ISBN’] = “287898988”><cfset aBook[‘title’] = “A Prayer for Owen Meany”><cfset aBook[‘author’] = “John Irving”><cfset aBook[‘publisher’] = “Ballantine Books”>

Page 33: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 33

Loop over a structure

Method 1<cfloop

collection="#aBook#" item="aProperty"><cfoutput>

#aProperty# is set to a #aBook[aProperty]#<br></cfoutput>

</cfloop>

Page 34: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 34

Loop over a structure

Method 2<cfloop

collection="#aBook#" item="aProperty"><cfoutput>

#aProperty# is set to a #Evaluate('aBook.' & aProperty)#<br>

</cfoutput></cfloop>

Page 35: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 35

By value

With most variables, a duplicate assignment creates a copy of the value in a memory location

Changes to greeting will not affect myGreeting

"Hello"

greeting

<cfset greeting = "Hello"><cfset myGreeting = greeting>

"Hello"

myGreeting

Page 36: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 36

By reference

With structures only, a duplicate assignment creates a pointer to the same memory location

Changes to book will affect myBook

structure

book

<cfset book = StructNew><cfset myBook = book>

myGreeting

Page 37: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 37

What will Pavorotti be singing?

<cfset aComposer=StructNew()><cfset aComposer.firstName = "Benjamin"><cfset aComposer.lastName = "Britten"><cfset aComposer.work = "Serenade for Tenor, Horn, and Strings">

<cfset concertChoice = aComposer>

<cfset aComposer.firstName = "Eric"><cfset aComposer.lastName = "Idle"><cfset aComposer.work = "Always Look On The Bright Side Of Life">

Today, Mr. Pavarotti will perform "#concertChoice.work#"

Page 38: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 38

Use Duplicate() for by value copies

<cfset aComposer=StructNew()><cfset aComposer.firstName = "Benjamin"><cfset aComposer.lastName = "Britten"><cfset aComposer.work = "Serenade for Tenor, Horn, and Strings">

<cfset concertChoice = Duplicate(aComposer)>

<cfset aComposer.firstName = "Eric"><cfset aComposer.lastName = "Idle"><cfset aComposer.work = "Always Look On The Bright Side Of Life">

Today, Mr. Pavarotti will perform "#concertChoice.work#"

Page 39: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 39

Structure functions

Duplicate()IsStruct()StructClear()StructCount()StructDelete()StructFind()

StructInsert()StructIsEmpty()StructKeyArray()StructKeyExists()StructKeyList()StructNew()StructUpdate()

Page 40: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 40

Duplicate()

<cfset newBook = Duplicate(Book)><cfset Book.price = "548788.99">

newBook.price 14.00

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 41: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 41

IsStruct()

IsStruct(Book) TRUE

<cfset FBrocks = TRUE>

IsStruct(FBrocks) FALSE

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 42: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 42

StructClear()

StructClear(Book)

IsStruct(Book) TRUE

Book.title error

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 43: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 43

StructCount()

StructCount(Book) 6

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 44: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 44

StructDelete()

StructDelete(Book,‘title’) ISBN 0345417976

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 45: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 45

StructFind()

StructFind(Book,‘title’) A Prayer for Owen Meany

StructFind(Book,‘reserved’) error

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 46: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 46

StructInsert()

StructInsert(Book,‘reserved’,4)

(The fourth parameter for this function, “allowoverwrite” is optional and defaults to FALSE. This option determines whether StructInsert() should overwrite the value of an existing key value.)

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

reserved 4

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 47: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 47

StructIsEmpty()

StructIsEmpty(Book) FALSE

StructIsEmpty(CD) error

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 48: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 48

StructKeyArray()

StructKeyArray(Book) ISBNAUTHOR FORMAT PUBLISHER TITLEPRICE

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 49: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 49

StructKeyExists()

StructKeyExists(Book,‘title’) TRUE

StructKeyExists(Book,‘reserved’) FALSE

StructKeyExists(Z,‘title’) error

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 50: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 50

StructKeyList()

StructKeyList(Book) “AUTHOR,ISBN,FORMAT,PRICE,PUBLISHER,TITLE”

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 51: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 51

StructNew()

<cfset Book = StructNew()>

StructIsEmpty(Book) TRUE

IsStruct(Book) TRUE

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 52: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 52

StructUpdate()

StructUpdate(Book,‘price’,16.95) ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 16.95

ISBN 0345417976

title A Prayer for Owen Meany

author John Irving

publisher Ballantine Books

format Paperback

price 14.00

Book: a structure

Page 53: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 53

Arrays

Definition: a numbered (or indexed) set of values in 1 to 3 dimensions

Page 54: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 54

Array limitations

Arrays can hold as values…– simple values– arrays– structures– result sets– lists

Page 55: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 55

1-D Arrays

Think of as a single row in a spreadsheet

5362 5557

[1] [2] [3] [4]

Page 56: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 56

2-D Arrays

Think of as multiple rows in a spreadsheet

5362 5557

[1] [2] [3] [4]

5948 52716465 5864

[1]

[2]

[3]

Page 57: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 57

3-D Arrays

Think of as a spreadsheet cube

6265 5461

[1] [2] [3] [4]

5948 52716465 5864

[1]

[2]

[3]

5259 65585948 52716465 5864

5362 55575948 52716465 5864

[1][2]

[3]

Page 58: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 58

Refer to array elements

Use the array name followed by the proper index/es in square brackets

5362 5557

[1] [2] [3] [4]

onedee

onedee[3] 55

5362 5557

[1] [2] [3] [4]

5948 52716465 5864

[1]

[2]

[3]twodee

twodee[3][4] 64

Page 59: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 59

Create a 1-D array

<cfset onedee = ArrayNew(1)><cfset onedee[1] = "57"><cfset onedee[2] = "62"><cfset onedee[3] = "55"><cfset onedee[4] = "53">

5362 5557onedee

Page 60: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 60

Loop over 1-D array

<cfloop from="1" to="#ArrayLen(aCart)#" index="aCol"><cfoutput>

#aCart[aCol]#<br></cfoutput>

</cfloop>

Page 61: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 61

Create a 2-D array

<cfset twodee = ArrayNew(2)><cfset twodee[1][1] = "57"><cfset twodee[1][2] = "62"><cfset twodee[1][3] = "55"><cfset twodee[1][4] = "53"><cfset twodee[2][1] = “71"><cfset twodee[2][2] = “48"><cfset twodee[2][3] = "52"><cfset twodee[2][4] = “59"><cfset twodee[3][1] = “64"><cfset twodee[3][2] = “65"><cfset twodee[3][3] = "58"><cfset twodee[3][4] = “64">

5362 55575948 52716465 5864

twodee

Page 62: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 62

Loop over 2-D array

<cfloop from="1" to="#ArrayLen(aCart)#" index="aRow"><cfloop

from="1" to="#ArrayLen(aCart[aRow])#" index="aCol"><cfoutput>

#aCart[aRow][aCol]# </cfoutput>

</cfloop><br></cfloop>

Page 63: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 63

Unbalanced arrays

Arrays can be “unbalanced”—meaning the number of columns may vary from row to row.

This is a valid CF array:

CF

CF

CF

CF

1

CF

CF

CF

CF

CF

Just for fun, write a loop that produces this array…

Page 64: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 64

Things to look out for

It is easy to “walk off” the end of an array—especially unbalanced arrays.

Make sure the array index exists—or CF will throw an

error.

21 3 4 5

<cfset myArr = anArray[6]>

myArr

Page 65: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 65

Array functions

ArrayAppend()ArrayAvg()ArrayClear()ArrayDeleteAt()ArrayIsEmpty()ArrayLen()ArrayMax()ArrayMin()

ArrayNew()ArrayPrepend()ArraySet()ArraySort()ArraySum()ArraySwap()ArrayToList()

Page 66: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 66

Use array functions on 2-D arrays

Made to work on 1-D arrays 2-D arrays can be thought of as a series of

stacked 1-D arrays

5362 5557

5948 5271

6465 5864

stacked1-D arrays

single2-D array

Page 67: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 67

Use array functions on 2-D arrays

If you want to use array functions on 2-D arrays, point at one of the stacked 1-D arrays by specifying which row you want the function to operate on

Example#ArraySum(twodee[3])#

5362 5557

5948 5271

6465 5864

Page 68: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 68

ArrayAppend()

A: 1-D arrayB: 2-D array

ArrayAppend(A,‘x’)

ArrayAppend(B,‘x’) ERROR

ArrayAppend(B[2],‘x’)

1 2 3 4

2 4 6 8

3 6 9 12

1 2 3 4 x

1 2 3 4

2 4 6 8

3 6 9 12

x

Page 69: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 69

ArrayAvg()

ArrayAvg(A) 2.5

ArrayAvg(B) ERROR

ArrayAvg(B[2]) 5

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 70: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 70

ArrayClear()

ArrayClear(A) YESArrayIsEmpty(A) YES

ArrayClear(B) YESArrayIsEmpty(B) YES

ArrayClear(B[2]) YESArrayIsEmpty(B[2]) YESArrayIsEmpty(B[3]) NO

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 71: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 71

ArrayDeleteAt()

ArrayDeleteAt(A,2)

ArrayDeleteAt(B,2)

ArrayDeleteAt(B[2],2)

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

1 3 4

1 2 3 4

3 6 9 12

1 2 3 4

2 6 8

3 6 9 12

Page 72: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 72

ArrayInsertAt()

ArrayInsertAt(A,3,‘x’)

ArrayInsertAt(B,2,‘x’) ERROR

ArrayInsertAt(B[2],2,‘x’)

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

1 2 3 4x

1 2 3 4

2 4 6 8

3 6 9 12

x

Page 73: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 73

ArrayIsEmpty()

See ArrayClear() function

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 74: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 74

ArrayMax()

ArrayMax( A ) 4

ArrayMax( B ) ERROR

ArrayMax( B[2] ) 8

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 75: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 75

ArrayMin()

ArrayMin(A) 1

ArrayMin(B) ERROR

ArrayMin(B[2]) 2

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 76: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 76

ArrayNew()

<cfset A = ArrayNew( 1 )>

<cfset B = ArrayNew(2 )>

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 77: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 77

ArrayPrepend()

ArrayPrepend(A,‘x’)

ArrayPrepend(B,‘x’) ERROR

ArrayPrepend(B[2],‘x’)

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

1 2 3 4x

1 2 3 4

2 4 6 8

3 6 9 12

x

Page 78: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 78

ArrayResize()

•Resets an array to a specified minimum number. •Used for performance gains on very large arrays

Page 79: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 79

ArraySet()

<cfset ArraySet(A,1,4,2 )>

<cfset ArraySet(B,1,4,2)> ERROR

<cfset ArraySet(B[2],1,4,2)>

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

2 2 2 2

2 2 2 2

Page 80: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 80

ArraySort()

ArraySort(A,‘numeric’,‘desc')

ArraySort(B,‘numeric,‘desc’) ERROR

ArraySort(B[2],‘numeric,‘desc’)

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

4 3 2 1

1 2 3 4

8 6 4 2

3 6 9 12

Page 81: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 81

ArraySum()

ArraySum(A) 10

ArraySum(B) ERROR

ArraySum(B[2]) 20

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 82: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 82

ArraySwap()

ArraySwap(A,3,2)

ArraySwap(B,3,2)

ArraySum(B[2],3,2)

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

1 3 2 4

1 2 3 4

3 6 9 12

2 4 6 8

1 2 3 4

2 6 4 8

3 6 9 12

Page 83: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 83

ArrayToList()

ArrayToList(A) “1,2,3,4”

ArrayToList(B) error

ArrayToList(B[2],‘|’) “2|4|6|8”

A: 1-D arrayB: 2-D array

1 2 3 4

2 4 6 8

3 6 9 12

Page 84: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 84

Lists

Definition: a list is a string consisting of meaningful data (or tokens) separated by delimiters

Page 85: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 85

Unlimited delimiters

Delimiters can be any single byte character

a,b,c (comma is default delimiter)

a|b|cmyPage.cfm?a=1&b=17552&c=TRUEabca b c

Page 86: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 86

List limitations

Lists can hold as values…– simple values– lists

Page 87: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 87

Read a value from a list

ListGetAt('a,b,c',2) b

Page 88: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 88

See if a value exists in a list

ListFind(myList,2) TRUE | FALSEListFindNoCase(myList,2) TRUE | FALSE

Page 89: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 89

Set the value of a list element

<cfset myList = "a,b,d"><cfset myList = ListSetAt(myList,3,'c')>

Page 90: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 90

Insert an element into a list

<cfset myList = "a,b,d"><cfset myList = ListInsertAt(myList,3,'c')>

Page 91: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 91

Delete an element from a list

<cfset myList = "a,b,d"><cfset myList = ListDeleteAt(myList,3)>

Page 92: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 92

Loop over a list

<cfset myList = "a,b,d"><cfoutput><cfloop list="#myList#" index="aListElement">

#aListElement#<br></cfloop></cfoutput>

Page 93: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 93

List functions

ListLen()ListPrepend()ListQualify()ListRest()ListSetAt()ListSort()ListToArray()ListValueCount()ListValueCountNoCase()

ListAppend()ListChangeDelims()ListContains()ListContainsNoCase()ListDeleteAt()ListFind()ListFindNoCase()ListFirst()ListGetAt()ListInsertAt()ListLast()

Page 94: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 94

ListAppend()

myNums 25,50,75<cfset myNums = ListAppend(myNums,100,',')>

myNums 25,50,75,100

Page 95: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 95

ListChangeDelims()

myNums 25,50,75<cfset myNums = ListChangeDelims(myNums,'+',',')>

myNums 25+50+75

Page 96: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 96

ListContains()

myWords apple,baker,charlieListContains(myWords,'BAKER',',') 0

Page 97: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 97

ListContainsNoCase()

myWords apple,baker,charlieListContainsNoCase(myWords,'BAKER',',') 2

ListContainsNoCase(myWords,'bak') 2

If you want to find elements (as opposed to sub-strings), use the ListFind() and ListFindNoCase() functions

Page 98: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 98

ListDeleteAt()

myNums 25,50,75<cfset myNums = ListDeleteAt(myNums,2,',')>

myNums 25,75

Page 99: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 99

ListFind()

myWords apple,baker,charlieListFind(myWords,'BAKER',',') 0ListFind(myWords,'baker',',') 2ListFind(myWords,'bak',',') 0

Page 100: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 100

ListFindNoCase()

myWords apple,baker,charlieListFind(myWords,'BAKER',',') 2ListFind(myWords,'baker',',') 2ListFind(myWords,'bak',',') 0

Page 101: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 101

ListFirst()

myWords apple,baker,charlieListFirst(myWords) apple

Page 102: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 102

ListGetAt()

myWords apple,baker,charlieListGetAt(myWords,3) charlie

Page 103: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 103

ListInsertAt()

myNums 25,50,75<cfset myNums = ListInsertAt(myNums,2,37,',')>

myNums 25,37,50,75

Page 104: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 104

ListLast()

myWords apple,baker,charlieListLast(myWords,',') charlie

Page 105: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 105

ListLen()

myWords apple,baker,charlieListLen(myWords,',') 3

Page 106: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 106

ListPrepend()

myNums 25,50,75<cfset myNums = ListPrepend(myNums,12,',')>

myNums 12,25,50,75

Page 107: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 107

ListQualify()

myWords apple,baker,charlie<cfset myWords = ListQualify(myWords,"'",',','ALL')>

myWords 'apple','baker','charlie'

Page 108: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 108

ListRest()

myWords apple,baker,charlieListRest(myWords,',') baker,charlie

Page 109: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 109

ListSetAt()

myNums 25,50,75<cfset myNums = ListSetAt(myNums,3,100',')>

myNums 25,50,100

Page 110: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 110

ListSort()

myNums 25,50,75<cfset numsDown = ListSort(myNums,'numeric','desc',',')>

myNums 75,50,25

myWords apple,baker,charlie<cfset wordsDown =

ListSort(myNums,'textnocase','desc',',')>

myWords charlie,baker, apple

Page 111: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 111

ListToArray()

myWords apple,baker,charlie<cfset arr = ListToArray(myWords)>

arr apple baker charlie

Page 112: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 112

ListValueCount()

myWords apple,baker,charlieListValueCount(myWords,'charlie',',') 1ListValueCount(myWords,'CHARLIE',',') 0

Page 113: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 113

ListValueCountNoCase()

myWords apple,baker,charlieListValueCount(myWords,'charlie',',') 1ListValueCount(myWords,'CHARLIE',',') 1

Page 114: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 114

Query Result Set

Definition: row(s) of values returned by an SQL query

Page 115: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 115

Query a database

<cfquery name=“query_name” datasource=“existing_datasource”>

…SQL Statement…</cfquery>

Page 116: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 116

Query properties

<cfquery datasource="cfsnippets" name="myQ">SELECT center_ID, name, city FROM Centers

</cfquery>

<cfoutput>The columns returned are #myQ.columnList#<cfif myQ.recordCount>

<cfloop query=“myQ”>I am now on row #currentRow#<br>

</cfloop></cfif></cfoutput>

Page 117: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 117

Loop over a query

Use either <cfoutput query=“query_name”>|#column_name#</cfoutput>

or <cfoutput>

<cfloop query=“myQ”>#column_name#

</cfloop></cfoutput>

Page 118: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 118

Get results of one column as a list

Use the ValueList() or QuotedValueList() function

<cfset publishers = ValueList(myQ.publisher)>

publishers Techspedition,Techspedition,Prentice Hall,Addison-Wesley,Sams

Page 119: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 119

Dereference a query value

<cfoutput>Your name is #UserInfo.firstName#

</cfoutput>

Page 120: ©2003 Hal Helms, Inc. Variables The Tao that can be told is not the eternal Tao. The name that can be named is no t the eternal Name. —Lao-Tzu

ColdFusion MX Foundations

©2003 Hal Helms, Inc. 120

Query functions

IsQuery()QueryAddColumn()QueryAddRow()QueryNew()QuerySetCell()QuotedValueList()ValueList()