Skip to main content
Inspiring
September 17, 2009
Answered

Comma delimited list

  • September 17, 2009
  • 2 replies
  • 1749 views

Hi,

In the following list, how can I keep the word Social, Behavioral, and Economic Sciences as one word instead of

Social

Behavioral

and Economic Sciences

<cfloop list="Biological Sciences, Social, Behavioral, and Economic Sciences" index="i" delimiters=",">

<cfoutput> #i# <br /></cfoutput>

</cfloop>

The list should look like:

Biological Sciences

Social, Behavioral, and Economic Sciences

Thanks!

This topic has been closed for replies.
Correct answer ilssac

Use something other then a comma to delimit the list.

<cfloop list="Biological Science| Social, Behavioral, and Economic Sciences" index="i" delimiters="|">

<cfoutput> #i# <br /></cfoutput>

</cfloop>

2 replies

BKBK
Community Expert
Community Expert
September 17, 2009

Assuming that you may not tamper with the given list, then there are functions you can use. For example, listFirst() and listRest().

<cfset list="Biological Sciences, Social, Behavioral, and Economic Sciences">
<cfoutput>#listFirst(list)#<br>
#listRest(list)#</cfoutput>

Inspiring
September 17, 2009

The problem here is that it is impossible to distinguish between whether the comma is delimiter, or whether it is part of the data.  So using list functions will not reliably work unless one already knows the composition of the list.

If there is a chance there will be commas in the data, then one must use a different delimiter.  For user-entered data, then using any keyable character for the delimiter is ill-advised.

I tend to use chr(30) - record separator - as my delimiter, as it is very unlikely to appear in use-entered data.

That said, CF lists are a crap multi-value data structure, and I avoid them where I can; preferring to use an array, struct or object as appropriate.

--

Adam

ilssac
ilssacCorrect answer
Inspiring
September 17, 2009

Use something other then a comma to delimit the list.

<cfloop list="Biological Science| Social, Behavioral, and Economic Sciences" index="i" delimiters="|">

<cfoutput> #i# <br /></cfoutput>

</cfloop>

jenn1Author
Inspiring
September 21, 2009

Thanks Ian!  It worked great!