Ted pottel
Ted pottel

Reputation: 6993

How to get rid of last comma when generating a list?

I'm writing a web service in ColdFusion. The problem is that I cannot figure out how to get rid of the comma after the last element. My code looks like this:

<cfoutput query="Attachments">
    #url#,
</cfoutput>

Which produces output like this (notice the trailing comma)

url1,url2,url3, 

How can I get rid of the trailing comma and produce this instead?

url1,url2,url3

Upvotes: 2

Views: 2090

Answers (2)

ale
ale

Reputation: 6430

Jake's answer is what's needed in this particular case.

For more generic cases, you can do this:

<cfloop ...>
  <cfset myList=listAppend(myList,value)>
</cfloop>

There's also a bit of trickery you can do since ColdFusion (by default) ignores empty list elements:

<cfset myList=arrayToList(listToArray(myList))>

Heck, even this'll work:

<cfset myList=listChangeDelims(myList , "," , ",")>

Of course, if you're not outputting the list as a string, you don't need to worry about that comma on the end since ColdFusion will just ignore the empty element. If you are outputting it as a string, here's yet another way to clean up that comma. It's not as reliable as the others, though.

<cfoutput>#left(trim(mylist),len(trim(mylist))-1)#</cfoutput>

Upvotes: 0

Jake Feasel
Jake Feasel

Reputation: 16945

This is an easy method:

<cfoutput>#ValueList(Attachments.url)#</cfoutput>

Upvotes: 8

Related Questions