code optimizing in coldfusoin, as String are immutable in Coldfusion

170 views Asked by At

I am new to Coldfusion and trying to build a string something like

<cfif qRoute.a IS NOT "">
      <cfset str= qRoute.a>
      <cfif qRoute.b IS NOT "">
        <cfset str= str& " /  "& qRoute.b>
      </cfif>  
      <cfif qRoute.c IS NOT "" >
        <cfset str= str& " /  "& qRoute.c>
      </cfif> 
    </cfif> 

But it seems to me to be very basic technique. Is there a better way to write the code.

1

There are 1 answers

0
Adam Cameron On BEST ANSWER

This would perhaps be an approach. it's a complete stand-alone repro, but the bit you want to look at is the listAppend() stuff:

<cfscript>
qRoute = queryNew("");
queryAddColumn(qRoute, "a", "varchar", ["","a2","a3","a4"]);
queryAddColumn(qRoute, "b", "varchar", ["","","b3","b4"]);
queryAddColumn(qRoute, "c", "varchar", ["","","","c4"]);
</cfscript>

<cfloop query="qRoute">
    <cfif not len(qRoute.a)>
        <cfcontinue>
    </cfif>
    <cfset str = "">
    <cfset str = listAppend(str, qRoute.a)>
    <cfset str = listAppend(str, qRoute.b)>
    <cfset str = listAppend(str, qRoute.c)>
    <cfset str = listChangeDelims(str, "/")>
    <cfoutput>[#str#]<br></cfoutput>
</cfloop>

On ColdFusion 9, this outputs:

[a2]
[a3/b3]
[a4/b4/c4]

Is that more what you're after?

The code for more recent versions of CFML would be much nicer, but you're kinda hamstrung by using an obsolete version of CF (which I'm sure is outwit your control, but it irks me to have to write such clumsy code)