View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > ArrayToList PreviousNext

ArrayToList

Converts a one-dimensional array to a list.

Delimited list, as a string.

Array functions, Conversion functions, List functions

ArrayToList(array [, delimiter ])

Parameter

Description

array

Name of array

delimiter

Character or multi-character string to separate list elements. Default: comma.

<h3>ArrayToList Example</h3>
<cfquery name = "GetEmployeeNames" datasource = "cfsnippets">
SELECT FirstName, LastName FROM Employees
</cfquery>
<!--- create an array --->
<cfset myArray = ArrayNew(1)>
<!--- loop through query, append names successively to last element --->
<cfloop query = "GetEmployeeNames">
   <cfset temp = ArrayAppend(myArray, "#FirstName# #LastName#")>
</cfloop>
<!--- show the resulting array as a list --->
<cfset myList = ArrayToList(myArray, ",")>
<!--- sort that array descending alphabetically --->
<cfset myAlphaArray = ArraySort(myArray, "textnocase", "desc")>
<!--- show the resulting alphabetized array as a list --->
<cfset myAlphaList = ArrayToList(myArray, ",")>
<!--- output the array as a list --->
<cfoutput>
   <p>The contents of the array are as follows:
   <p>#myList#
   <p>This array, alphabetized by first name (descending):
   <p>#myAlphaList#
   <p>This array has #ArrayLen(MyArray)# elements.
</cfoutput>

Contents > CFML Reference > ColdFusion Functions > ArrayToList PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


knallore said on May 27, 2004 at 6:08 AM :
Adding a blank line before each comment could make the example code more readable.
eokyere said on Nov 10, 2004 at 9:05 PM :
if you want blank lines, copy the code to your editor and add them yourself, otherwise, keep your noise to yourself
katdragon said on Dec 1, 2004 at 7:27 AM :
IMHO, the comment about readbale code is dead on. Don't blow it off just cause the code isn't as readable as some would like.

In any case, that's not my comment. In the code above, there are these lines:

<!--- show the resulting array as a list --->
<cfset myList = ArrayToList(myArray, ",")>
<!--- sort that array descending alphabetically --->
<cfset myAlphaArray = ArraySort(myArray, "textnocase", "desc")>
<!--- show the resulting alphabetized array as a list --->
<cfset myAlphaList = ArrayToList(myArray, ",")>

Then below, it outputs myAlphaList as an order list. However, it isn't. the cfset that assigns the value to myAlphaList should be this instead:

<cfset myAlphaList = ArrayToList(myAlphaArray, ",")>
jrunrandy said on Dec 1, 2004 at 10:00 AM :
I tried this and I don't think you are correct. The ArraySort function returns a Boolean value, so MyAlphaList actually contains a Boolean. The ArraySort function works in-place on the referenced array.

You might, however, argue (successfully, I'm afraid) that the variable names are misleading.
CFDan said on May 2, 2005 at 11:46 AM :
If the first element of the array to be converted is empty, the element does not appear on the list. However any other empty array elements appear as empty list items.

In other words:
myArray[1] = 'one'
myArray[2] = 'two'
myArray[3] = 'three'

will output as a list like this: one,two,three

but if you have this

myArray[1] = ''
myArray[2] = 'two'
myArray[3] = 'three'

you only get the following: two,three

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functa19.htm