1

I am trying to create a comma separated list inside a loop, but I think I am missing something. When I dump item_id_list, I just get items separated by a space - not a comma. Can anyone point what am I missing?

<cfloop array="data" index="doc">
    <cfif structKeyExists(doc, "id") >
        <cfset the_item_id = doc.id /> 
    </cfif>
  <cfset item_id_list = ''/>
  <cfset item_id_list = listappend(item_id_list,'#the_item_id#',',')/>
</cfloop>
1
  • just get items separated by a space That could not happen with the code above, because it overwrites the list each time. You need to post the actual code causing the problem. Commented Jul 20, 2016 at 4:06

2 Answers 2

10

Create the list outside of the loop:

<cfset item_id_list = "" />
<cfloop array="#data#" index="doc">
   <cfif structKeyExists(doc, "id") >
       <cfset item_id_list = listappend(item_id_list, doc.id, ",") />
   </cfif>
</cfloop>
Sign up to request clarification or add additional context in comments.

Comments

0

Will this work?

<cfset item_id_list = ArrayToList(data.id)>

EDIT: Oops. Can't reference a struct in an array like that. Instead try:

<cfscript>
    item_id_list = "" ;
    for (row in data) {
        if (structkeyexists(row,"id") ) {
            item_id_list = listappend(item_id_list,row.id) ;
        }
    }
</cfscript>

For loops like this, cfscript syntax is easier to see.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.