Skip to main content
March 21, 2007
Question

Setting a comma in a dynamic list build

  • March 21, 2007
  • 8 replies
  • 812 views
Hi Guys, can anyone please help me with this issue. I want to take a list and find the last characters in the list and then build another list. With this other list I am having a problem not dropping a comma after the last element.

I am expecting this y,n I am getting y,n,

Here is my code.

<cfset initials = " ">
<cfset george = "hary,martin">
<cfset showComma = false>
<Cfloop index="name" list="#george#" delimiters=",">
<cfset initials = initials & right(name,1) & "<cfif showComma>,<cfelse><cfset showComma = true></cfif>">
</cfloop>
This topic has been closed for replies.

8 replies

March 21, 2007
<cfset initials = "">
<cfset george = "pighg1,pighg2,PIGHGB">
<Cfloop index="name" list="#george#" delimiters=",">
<cfif IsNumeric(right(name,1))>
<cfif Len(initials) EQ 0>
<cfset initials = right(name,1)>
<cfelse>
<cfset initials = initials & "," & right(name,1)>
</cfif>
</cfif>
</cfloop>
<cfdump var="#initials#">
<cfset george2 = (ListToArray(temp2, ","))>
<cfset count = ArrayMax(george2)>
<cfdump var="#count#">
March 21, 2007
Well, here is what I am trying to further do. I want to use the ListToArray function and then use the ArrayMax function to find the largest value. The problem is that when I have a character other than a number in the Array the ArrayMax function throws an error. Here is my code.

<cfset initials = "">
<cfset george = "pighg1,pighg2,PIGHGB">
<Cfloop index="name" list="#george#" delimiters=",">
<cfset initials = initials & right(name,1)>
<cfif ListLast(george) neq name><cfset initials = initials & ","></cfif>
</cfloop>
<!---I would like to replace that b in the ListcontainsNoCase function with a REGEX for [a-z] the problem is that it throws an error --->
<cfset takeAway = ListContainsNoCase(initials, "b")>
<cfset temp2 = ListDeleteAt(initials, takeAway, ",")>
<cfdump var="#temp2#">
<cfset george2 = (ListToArray(temp2, ","))>
<cfset count = ArrayMax(george2)>
<cfdump var="#count#" >
March 21, 2007
How can I add a regex to the ListContainsNoCase Function?
<cfset takeAway = ListContainsNoCase(initials, [A-Z])>
March 21, 2007
How can I remove non numerics from my list?
March 21, 2007
Can you be specific?
March 21, 2007
My Bad, the second code is working.
March 21, 2007
Hi I tried the last suggestion and this is what I got.
y,n y,n

I am expecting y,n
March 21, 2007
<cfset initials = "">
<cfset george = "hary,martin">
<Cfloop index="name" list="#george#" delimiters=",">
<cfset initials = initials & right(name,1)>
<cfif ListLast(george) neq name><cfset initials = initials & ","></cfif>
</cfloop>
<cfdump var="#initials#">
Participating Frequently
March 21, 2007
The trick is to add the comma before the element in the list. Set a variable to test for the first iteration. On the first iteration, reset the variable, but don't write the comma. After that, append <comma><listElement> to the list.