• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Setting a comma in a dynamic list build

Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

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>
TOPICS
Advanced techniques

Views

726

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

<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#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

Hi I tried the last suggestion and this is what I got.
y,n y,n

I am expecting y,n

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

My Bad, the second code is working.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

How can I remove non numerics from my list?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

Can you be specific?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

How can I add a regex to the ListContainsNoCase Function?
<cfset takeAway = ListContainsNoCase(initials, [A-Z])>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

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#" >

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 21, 2007 Mar 21, 2007

Copy link to clipboard

Copied

LATEST
<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#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation