Using Lists


As you have seen thus far, lists are very easy to use and are well suited for simple access to grouped data. Lists were designed to be simple; they were not designed to be efficient. Therefore, it is important to know when to use lists and when not to.

When to Use Lists (and When Not To)

Lists are designed primarily to be used with data that is already in list format. Two key examples are as follows:

  • HTML form field values are returned in list format when multiple values exist for the same field name (for example, two HTML form controls have the same name, or a control allowing multiple selections is used).

  • SQL uses the list format for sets of values (for example, in an IN clause).

Form controls and variables are covered in detail in Chapter 9, "FORM Variables." The SQL IN clause is covered in Chapter 44, "Basic SQL."


When data is already in a list format, or needs to be, you should use lists and list functions.

Lists are not designed for complex or frequent processing; they just don't perform well enough for that. Arrays and structures are far better suited for that task.

TIP

You can convert lists into arrays and back again by using the ListToArray() and ArrayToList() functions.


Sorting Lists

You can sort lists by using the ListSort() function, and you must specify one of three sort types:

  • numeric should be used for lists containing only numbers (and should never be used if any elements are not numeric).

  • text performs case-sensitive alphabetical sorting (A before a, 1 before 10, 2 after 1 but before 20, and numbers before letters).

  • textnocase performs an alphabetical sort that is not case sensitive.

An optional sort order may also be specified; the default is asc (ascending), and the alternative is desc (descending).

Looping Through Lists

In addition to accessing lists via functions as shown previously, you also can use CFML to loop through lists with <cfloop>. Like the list functions, <cfloop> supports alternative (and multiple) delimiters. The following code snippet displays the states list in an HTML unordered list:

 <!--- Start unordered list ---> <ul> <!--- Loop through "states" ---> <cfloop index="i" list="#states#">  <!--- Write this element --->  <cfoutput><li>#i#</li></cfoutput> </cfloop> <!--- End unordered list ---> </ul> 

The list must be passed to <cfloop> in the list attribute; the variable name specified in the index attribute will contain the appropriate element in each loop iteration.

Looping and the <cfloop> tag were covered in detail in Chapter 4, "Looping."


TIP

Another way to apply formatting to list elements (without needing to loop through them) is to use the Replace() and ReplaceList() functions to replace all delimiters with appropriate HTML code.


Nested Lists

Although the ColdFusion documentation states that lists cannot be nested, technically they actually can. The trick is to use different delimiters for the inner and outer lists. For example, the following list has three elements if the default delimiter is used, and seven elements if a space is used as the delimiter:

 <cfset list="a b c,1 2 3,x y z"> 

But accessed within nested <cfloop> tags, the same list can be used as a nested listthree lists each with three elements. The outer list is delimited by commas, and each inner list is delimited by spaces.

TIP

This kind of list processing is especially useful when you're working with comma-delimited imported data files (sometimes called CSV files). A file can be read into a variable (using <cffile>), and the entire file can be treated as a list delimited by carriage-return and line-feed characters (ASCII characters 13 and 10, respectively). This way, each line in the file can be accessed individually as a list element. The data format within each line is comma delimited, so the list functions can be used to process and extract the individual elements easily.


Special Lists

Several CFML functions return data in list format (comma delimited). They include ValueList() and QuotedValueList(), which return query columns in list form so that they can be easily used in additional SQL statements, and functions like GetClientVariableList(), which returns a list of all CLIENT variables for the current user.

In addition, several variables are always formatted as lists (again, for simplicity's sake). They include query.ColumnList, which lists the columns returned in a query, and SERVER.ColdFusion.SupportedLocales, which lists the locales supported by ColdFusion.

You can manipulate all these lists by using the functions and loops discussed earlier.

TIP

Lists that need to be accessed frequently, particularly those that persist across requests, should be converted into arrays (or structures) to improve performance.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net