Skip to main content
Known Participant
June 21, 2006
Answered

Eliminating comma at end of output

My current output of dynamic names in my variable (called dynNames) has a comma at the end.

I need to eliminate the last comma:
Jones, Smith, Baker, Johnson,

to look like this:

Jones, Smith, Baker, Johnson


My attempt to eliminate the last comma doesnt work because it eliminates all the commas:


<cfset myNames = REReplace(dynNames, ",$" "", "All")>

<cfoutput>#myNames</cfoutput>


Output from my bad attempt:
Jones Smith Baker Johnson

Please advise.
    This topic is closed to new replies. Start a new post to keep the conversation going.
    Kronin555Correct answer
    Participating Frequently
    June 22, 2006
    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfset dynNames = "">
    <cfoutput>
    <cfset dynNames = listappend(dynnames,names)>
    </cfoutput>
    #dynNames#
    </cfoutput>

    12 replies

    FloridaAuthor
    Known Participant
    June 22, 2006
    Thanks it now works.
    Translate
    Inspiring
    June 22, 2006
    Have you tried something like this:

    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfoutput>
    #names#
    <cfif otherquery.currentrow lt otherquery.recordcount>
    ,
    </cfif>
    </cfoutput>
    </cfoutput>

    --
    Bryan Ashcraft (remove brain to reply)
    Web Application Developer
    Wright Medical Technologies, Inc.
    ------------------------------------------------------------------
    Macromedia Certified Dreamweaver Developer
    Adobe Community Expert (DW) :: http://www.adobe.com/communities/experts/


    "Florida" <webforumsuser@macromedia.com> wrote in message
    news:e7ecq2$49o$1@forums.macromedia.com...
    > The id and names are from different tables where the query
    > fetches an id that someone enters in a form and fetches the
    > id
    > and names. I have to group the otherquery by id so I can fetch all
    > id listings and get the names for each id.
    >
    >
    > Here is how I currently have it and it works great but the comma at the
    > end is
    > the only problem:
    >
    > <cfoutput query="otherquery" group="id">
    > #id#<br>
    > <cfoutput>
    > #names#,
    > </cfoutput>
    > </cfoutput>
    >

    >
    > Output:
    > Thompson, Jones, Williams,
    >


    Translate
    FloridaAuthor
    Known Participant
    June 22, 2006
    The id and names are from different tables where the query fetches an id that someone enters in a form and fetches the id and names. I have to group the otherquery by id so I can fetch all id listings and get the names for each id.


    Here is how I currently have it and it works great but the comma at the end is the only problem:

    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfoutput>
    #names#,
    </cfoutput>
    </cfoutput>


    Output:
    Thompson, Jones, Williams,
    Translate
    Kronin555Correct answer
    Participating Frequently
    June 22, 2006
    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfset dynNames = "">
    <cfoutput>
    <cfset dynNames = listappend(dynnames,names)>
    </cfoutput>
    #dynNames#
    </cfoutput>
    Translate
    FloridaAuthor
    Known Participant
    June 22, 2006
    Thanks,

    It still adds commas to all such as this:
    Jones, Carson, Baker,



    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfoutput>
    <cfset dynNames = "">
    <cfset dynNames = ListAppend(dynNames,names)>
    </cfoutput>
    <.cfoutput>


    Translate
    Participating Frequently
    June 22, 2006
    Something's missing from what you're posting. Could you please post the query and loop that generates this list, in entirety?

    The only time ListAppend adds an empty item to the list (which is what's happening, if you're still getting a trailing comma) is when you call ListAppend and pass it a null or empty string.

    The other way to accomplish this, since you're pulling out of a query, is to call ValueList on the query column you want turned into a list. Not sure if it'll work in your current code, as you still haven't posted enough of it for us to discern that.
    Translate
    Participating Frequently
    June 21, 2006
    > Should I do this another way?

    Yeah, use ListAppend().
    <cfset dynNames = ListAppend(dynNames,names)>
    Translate
    FloridaAuthor
    Known Participant
    June 21, 2006
    Thanks,

    I think my problem is this is in a nested cfoutput.

    I am taking the data which is in variable names that looks like this as an output:
    Jones Rivers Baker Johnson

    and putting comma delimeters on them like this:
    <i>
    <cfset dynNames = "#names#,">
    </i>
    so my output will have comma delimeters. The problem is I dont want a comma at the end which is what is happening:
    Jones, Rivers, Baker, Johnson,

    I tried all suggestions in this thread and all of them knock off all the commas but I only need the last comma knocked off. I assume I have an output issue.
    Should I do this another way? If so please advise.
    Translate
    Participating Frequently
    June 21, 2006
    Why not just use ListAppend() rather than your string append?
    Translate
    Inspiring
    June 21, 2006
    <cfset mylist = "1,2,3,4,5," />
    <cfset newlist = left(mylist,len(mylist)-1) />
    #newlist#

    --
    Bryan Ashcraft (remove brain to reply)
    Web Application Developer
    Wright Medical Technologies, Inc.
    ------------------------------------------------------------------
    Macromedia Certified Dreamweaver Developer
    Adobe Community Expert (DW) :: http://www.adobe.com/communities/experts/


    "Florida" <webforumsuser@macromedia.com> wrote in message
    news:e7blqf$ksm$1@forums.macromedia.com...
    > My current output of dynamic names in my variable (called dynNames)
    > has
    > a comma at the end.
    >
    > I need to eliminate the last comma:
    > Jones, Smith, Baker, Johnson,
    >
    > to look like this:
    >
    > Jones, Smith, Baker, Johnson
    >
    >
    > My attempt to eliminate the last comma doesnt work because it eliminates
    > all
    > the commas:
    >
    >
    > <cfset myNames = REReplace(dynNames, ",$" "", "All")>
    >
    > <cfoutput>#myNames</cfoutput>
    >

    >
    > Output from my bad attempt:
    > Jones Smith Baker Johnson
    >
    > Please advise.
    >


    Translate
    FloridaAuthor
    Known Participant
    June 21, 2006
    Thanks,

    Sorry I didnt show my entire script because it still doesnt work.
    I have the names that comes out where I put a comma in between each one but the last one shouldnt have one:


    <cfoutput query="otherquery" group="id">
    #id#<br>
    <cfoutput>
    <cfset dynNames = "#names#,">

    <CFSET bar = Mid(dynNames, 1, Len(dynNames) - 1)>
    #bar#
    </cfoutput>
    <cfoutput>
    Translate
    June 21, 2006
    See below.
    Translate