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:
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 ListsYou can sort lists by using the ListSort() function, and you must specify one of three sort types:
An optional sort order may also be specified; the default is asc (ascending), and the alternative is desc (descending). Looping Through ListsIn 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 ListsAlthough 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 ListsSeveral 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. |