Skip to main content
Inspiring
August 20, 2017
Question

cfloop : to annotate the different elements of a list

  • August 20, 2017
  • 1 reply
  • 342 views

Hello,

I would like to be able to annotate the different elements of a list made with a cfloop.

I do not know how to do it.

Thank you for your help.

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    August 21, 2017

    It's unclear what you mean by "annotate the different elements of a list". By definition, each element in a list has a position. Couldn't you use that as annotation?

    In any case, you could store the list elements in an array. It would then take just one function call to convert back to list. The thinking goes like this

    <cfset elements = arrayNew(1)>

    <cfloop index="idx" from="1" to="5">

        <cfswitch expression="#idx#">

            <cfcase value="1" >

                <cfset elements[idx] = "Genre">

            </cfcase>

            <cfcase value="2" >

                <cfset elements[idx] = "Nom">

            </cfcase>

            <cfcase value="3" >

                <cfset elements[idx] = "Prénom">

            </cfcase>

            <cfcase value="4" >

                <cfset elements[idx] = "Code postal">

            </cfcase>

            <cfcase value="5" >

                <cfset elements[idx] = "Ville :">

            </cfcase>          

        </cfswitch>

    </cfloop>

    <cfoutput>#arrayToList(elements)#</cfoutput>