Search code examples
coldfusioncfloop

cfloop index output in function call


I have a loop which I need to add in the index to a function call. How can I do this?

Here is what I tried but it fails

<cfloop index="i" from="1" to="#arrayLen(test)#">
    #session_ID & i &.getSessionCount()#
</cfloop>

The index of the loop should output so that each iteration of the loop the line would look like this:

#session_ID1.getSessionCount()#
#session_ID2.getSessionCount()#
#session_ID3.getSessionCount()#
#session_ID4.getSessionCount()#

and so on.


Solution

  • If one needs to create a variable name dynamically, then use associative array notation rather than dot notation, and reference the variable via the scope that it's in. EG:

    <cfloop index="i" from="1" to="#arrayLen(test)#">
        <cfset result = variables["session_ID" & i].getSessionCount()>
    </cfloop>