×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Eliminating last comma in my dynamic output
4

Eliminating last comma in my dynamic output

Eliminating last comma in my dynamic output

(OP)
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:

CODE

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

<cfoutput>#myNames</cfoutput>

Output from my bad attempt:  
Jones Smith Baker Johnson

Please advise.

RE: Eliminating last comma in my dynamic output

Remove the "ALL" statement.
<cfset myNames = REReplace(dynNames, ",$", "")>

This was my test:

CODE

<cfset one = "Jones, Smith, Baker, Johnson,">
<cfset myNames = REReplace(one, ",$", "")>
<cfoutput>
#one#<br>
#myNames#
</cfoutput>

The display comes as you want.

____________________________________
Just Imagine.
http://www.imajinarts.com

RE: Eliminating last comma in my dynamic output

(OP)
Thanks,

It still gets rid of all my commas because
it actually outputs from a nested output.
I have the names output that shows like this from the database:
Jones Carson Richards

But I put in a comma to show better.  The problem is I dont want the comma to show after the last name.

CODE

<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = "#names#,">
<cfset myNames = REReplace(dynNames, ",$", "")>
#myNames#
</cfoutput>
<cfoutput>

RE: Eliminating last comma in my dynamic output

you can also use ValueList().

<cfset myNames = ValueList(otherquery.dynNames)>

www.jetansolutions.com

RE: Eliminating last comma in my dynamic output

(OP)
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:

CODE

<cfset dynNames = "#names#,">
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.

RE: Eliminating last comma in my dynamic output

if this is in a loop/output query you can also do

<cfset dynNames = '>

<cfloop ...> OR <cfoutput query="..">
   <cfset dynNames = dynNames&','&#names#>
</cfloop>  / </cfoutput>


hope it helps...

www.jetansolutions.com

RE: Eliminating last comma in my dynamic output

(OP)
Thanks, this is close:

CODE

<cfset dynNames = ''>

<cfoutput query="..">
   <cfset dynNames = dynNames&','&#names#>
</cfoutput>

It outputs:
,Jones, Rogers, Baker

How do I get rid of first comma?

RE: Eliminating last comma in my dynamic output

oops. my bad
<cfset dynNames = '>

<cfoutput query="..">
   <cfif ListLen(dynNames) EQ 0>
       <cfset dynNames = dynNames&#names#>
   <Cfelse>
       <cfset dynNames = dynNames&','&#names#>
   </cfif>
</cfoutput>

www.jetansolutions.com

RE: Eliminating last comma in my dynamic output

(OP)
Thanks, the output still eliminates all my commas giving me this:  Jones Carson Baker Smith
It must be something for how I have my nested output which I need.

CODE

<cfoutput query="otherquery" group="id">
#id#<br>
<cfoutput>
<cfset dynNames = ''>
   <cfif ListLen(dynNames) EQ 0>
       <cfset dynNames = dynNames&#names#>
   <Cfelse>
       <cfset dynNames = dynNames&','&#names#>
   </cfif>
</cfoutput>
<cfoutput>

RE: Eliminating last comma in my dynamic output

what's wrong with using ValueList() as previously suggested?

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

(OP)
ValueList(myquery.dynNames) outputs invalid data names in my output:


CODE

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

RE: Eliminating last comma in my dynamic output

ValueList operates on the entire column, so you should not put in inside a CFOUTPUT loop

do this instead --

<cfset dynNames = ValueList(otherquery.names)>
<cfoutput>
#dynNames#
</cfoutput>

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

(OP)
Thanks I tried that and it still gives invalid data where it duplicates the names.

CODE

<!--- Also tried putting it here:  <cfset dynNames = ValueList(otherquery.names)>  --->
<cfoutput query="otherquery" group="id">
#id#<br>
<cfset dynNames = ValueList(otherquery.names)>
<cfoutput>
#dynNames#
</cfoutput>
</cfoutput>

RE: Eliminating last comma in my dynamic output

invalid data? duplicates names?

what do you get when you run this --

CODE

<cfquery name="otherquery" datasource="#foo#">
 select names from  ...
</cfquery>

<cfset dynNames = ValueList(otherquery.names)>  

<cfoutput>
#dynNames#
</cfoutput>
you don't by any chance have comma-delimited names stored in the names column, do you?

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

(OP)
If I run your example it will show different data so I assume it is something unique to my query.

No I dont have comma delimited names in the names column.

I appreciate all the input and I assume I will have to make my output with just spaces in between the names to solve my problem?

CODE

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

RE: Eliminating last comma in my dynamic output

okay, well, i don't understand what you're doing, so good luck with your "other data"

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

can you do a favor and try this...

<cfoutput query="otherquery" group="id">
#id#<br>
<!--- this used to be reseting to ' after every name, it was in the loop of the nested cfoutput
<cfset dynNames = '>

<cfoutput>
   <cfif ListLen(dynNames) EQ 0>
       <!--- we know cynNames is ', so no reason for dynNames&names --->
       <cfset dynNames = names>
   <cfelse>
       <!--- working with a list, use a list function --->
       <cfset dynNames = listAppend(dynNames,names)>
   </cfif>
</cfoutput>

<cfoutput>

valuelist() won't work because of the CFOUTPUT with group... he wants a list of names PER id. valuelist give the ENTIRE list of names to EVERY id.
 

Kevin
http://www.lot-o-nothin.com/cfml/

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!

RE: Eliminating last comma in my dynamic output

Quote:

valuelist() won't work because of the CFOUTPUT with group... he wants a list of names PER id. valuelist give the ENTIRE list of names to EVERY id.
aha, okay, my bad, i guess i should've figured that out, but i didn't, sorry

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

Jeez Rudy, get it together!! Just kidding, I figured you just mis-read or read to fast.

Kevin
http://www.lot-o-nothin.com/cfml/

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!

RE: Eliminating last comma in my dynamic output

:)

yeah, it's been a rough month for me

old computer died, had to get a new one

had to upgrade myself on all sorts of software

on the plus side, i was finally able to install cf on my own machine, and actually hook it up to a local mysql database so that i can test cf queries...

(i had never succeeded in installing cf in 8 years of using it -- i had always just uploaded my .cfm files to my host)

also, being forced to set up a new machine for my own development purposes inevitably meant that i had to find some new software to replace things i had been using that you can't get anymore, such as the free version of mysql-front, which i very joyfully was able to replace with Heidi SQL (a spinoff of mysql-front) -- if you haven't tried it, it's way better than phpmyadmin)

r937.com | rudy.ca

RE: Eliminating last comma in my dynamic output

(OP)
Thanks for all the help.

This is what finally worked.

I am not sure what ListAppend() function does?
I assume it adds each record find to a list?

CODE

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

RE: Eliminating last comma in my dynamic output

(OP)
Sorry typo on one line.

Here is what worked:

CODE

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close