Understanding Arrays


Arrays, like lists, store multiple values within a single variable. Arrays differ from lists in two important ways:

  • Arrays, unlike lists, are an actual data type, with a special internal format that ensures that they perform very efficiently.

  • Although arrays can be used to store multiple strings or numbers (like lists), they can also be used to store complex data like arrays, queries, or structures.

NOTE

ColdFusion stores queries, structures, and arrays as complex data. ColdFusion stores strings and numbers as simple data.


When to Use Arrays

You should use arrays when the order of something is crucial. A shopping cart, for example, is the perfect candidate for an array because each item is referenced according to the order in which the buyer chose it. Arrays should also be used where descriptive statistics (average, sum, min/max, count) need to be performed on a series of values.

When Not to Use Arrays

Arrays are not ideal for all situations. Because arrays are complex data, a complete array can't be printed or passed without the use of <cfloop>. For situations in which you need to pass a set of data between HTML form pages or print it on an HTML page, lists are preferred.

For situations in which the order of values does not matter and simple calculations such as averaging or summation are not necessary, a structure is a better alternative. Similarly, if array members (elements) must be named explicitly, then structures should be used.

Arrays are best suited for grouping related data (for example, names of students, grocery lists, and clothing sizes).

CAUTION

You may hear the term associative array usedthese are structures, not arrays.


Structures will be covered in detail in Chapter 15, "Structures." <cfloop> was described in Chapter 4, "Looping," and lists were discussed in Chapter 13, "Lists."




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