Skip to main content
Inspiring
Aug 20, 2017 12:38 PM UTC
Question

cfloop : to annotate the different elements of a list

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 is closed to new replies. Start a new post to keep the conversation going.

    1 reply

    BKBK
    Community Expert
    Community Expert
    Aug 21, 2017 12:14 PM UTC

    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>

    Translate