Question

I am running ColdFusion Enterprise 9.0.1.274733 on Windows Server 2008 R2 with Java 1.6.0_22.

I am calling a SOAP web service that returns various data to me. One of the data elements contains a code consisting of numbers with leading zeros. I am trying to convert that code into a text description but am having problems with the leading zeros. I have tried using the cfswitch tag as well as various things with the cfif tag. They are behaving a bit differently. I am looking for some suggestions on how to best handle these codes.

Here is an example of the code to description lookup:

Code     Description
 01      Automobile
 010     Personal Automobile
 011     Commercial Automobile
 02      Home
 03      Boat
 10      Life
 11      Umbrella

I initially attempted to use a cfswitch block to handle this but found that it treats the code as an integer. So as far as cfswitch is concerned; 010 is equal to 10. I have also tried using cfif. It also appears to be converting the value during the comparison. So as far as cfif is concerned the opposite is true; 10 is equal to 010.

How do you guys handle this issue?

Here is some example code that shows what is happening:

<html>
    <head><title>Test</title></head>
<body>
    <h3>Test</h3>
    <cfset testvals = "01,010,011,10,11,12" />
    <cfoutput>
    <div>
        <cfloop list="#testvals#" index="testval">
            <p>testval = [#testval#]
            <cfswitch expression="#testval#">
                <cfcase value="01">    <cfset desc="matches 01" />    </cfcase>
                <!---<cfcase value="010">    <cfset desc="matches 010" /></cfcase> --->
                <!---<cfcase value="011">    <cfset desc="matches 011" /></cfcase> --->
                <cfcase value="02">    <cfset desc="matches 02" />    </cfcase>
                <cfcase value="03">    <cfset desc="matches 03" />    </cfcase>
                <cfcase value="04">    <cfset desc="matches 04" />    </cfcase>
                <cfcase value="08">    <cfset desc="matches 08" />    </cfcase>
                <cfcase value="09">    <cfset desc="matches 09" />    </cfcase>
                <cfcase value="10">    <cfset desc="matches 10" />    </cfcase>
                <cfcase value="11">    <cfset desc="matches 11" />    </cfcase>
                <cfcase value="12">    <cfset desc="matches 12" />    </cfcase>
                <cfdefaultcase>        <cfset desc="no match" />    </cfdefaultcase>
            </cfswitch>
            <br />cfswitch: #desc#

            <cfif testval EQ "01">
                <cfset desc="matches 01" />
            <cfelseif testval EQ "010">
                <cfset desc="matches 010" />
            <cfelseif testval EQ "011">
                <cfset desc="matches 011" />
            <cfelseif testval EQ "02">
                <cfset desc="matches 02" />
            <cfelseif testval EQ "03">
                <cfset desc="matches 03" />
            <cfelseif testval EQ "04">
                <cfset desc="matches 04" />
            <cfelseif testval EQ "08">
                <cfset desc="matches 08" />
            <cfelseif testval EQ "09">
                <cfset desc="matches 09" />
            <cfelseif testval EQ "10">
                <cfset desc="matches 10" />
            <cfelseif testval EQ "11">
                <cfset desc="matches 11" />
            <cfelseif testval EQ "12">
                <cfset desc="matches 12" />
            <cfelse>
                <cfset desc="no match" />
            </cfif>
            <br />cfif: #desc#

            <cfif toString(testval) EQ "01">
                <cfset desc="matches 01" />
            <cfelseif toString(testval) EQ "010">
                <cfset desc="matches 010" />
            <cfelseif toString(testval) EQ "011">
                <cfset desc="matches 011" />
            <cfelseif toString(testval) EQ "02">
                <cfset desc="matches 02" />
            <cfelseif toString(testval) EQ "03">
                <cfset desc="matches 03" />
            <cfelseif toString(testval) EQ "04">
                <cfset desc="matches 04" />
            <cfelseif toString(testval) EQ "08">
                <cfset desc="matches 08" />
            <cfelseif toString(testval) EQ "09">
                <cfset desc="matches 09" />
            <cfelseif toString(testval) EQ "10">
                <cfset desc="matches 10" />
            <cfelseif toString(testval) EQ "11">
                <cfset desc="matches 11" />
            <cfelseif toString(testval) EQ "12">
                <cfset desc="matches 12" />
            <cfelse>
                <cfset desc="no match" />
            </cfif>
            <br />tostring: #desc#
            </p>
        </cfloop>
    </div>
    </cfoutput>
</body>
</html>

Note that I had to comment out the cfcase tags for the values of 010 and 011 to avoid an error. If those are in the cfswitch then I get this error: Context validation error for the cfcase tag. The cfswitch tag has a duplicate cfcase tag for value 10.0. The error occurred on line -1.

Here is the output from the example code:

Test

testval = [01] 
cfswitch: matches 01 
    cfif: matches 01 
tostring: matches 01

testval = [010] 
cfswitch: matches 10     // trying to avoid this
    cfif: matches 010 
tostring: matches 010

testval = [011] 
cfswitch: matches 11     // trying to avoid this
    cfif: matches 011 
tostring: matches 011

testval = [10] 
cfswitch: matches 10 
    cfif: matches 010    // trying to avoid this
tostring: matches 010    // trying to avoid this

testval = [11] 
cfswitch: matches 11 
    cfif: matches 011    // trying to avoid this
tostring: matches 011    // trying to avoid this

testval = [12] 
cfswitch: matches 12 
    cfif: matches 12 
tostring: matches 12
Was it helpful?

Solution

I often use compare() to avoid undesired implicit conversion problems:

   <cfif compare("010", testVal) EQ 0>
       matches 010
   </cfif>

But if you are just returning a single value, have you considered using a structure to do a simple look-up instead?

 <cfset lookup = { "010"="matches 010", ...} > 
 <cfif structKeyExists(lookup, testVal)>
     do something with #lookup[ testVal ]# ...
 </cfif>

So as far as cfswitch is concerned; 010 is equal to 10

Edit: That sounds a little buggy IMO. The documentation mentions a similar issue with 0 versus 00 and states that it was fixed. But the error still occurs in CF 9. So you may want to submit a bug report.

The value "00" was also evaluated to the value 0. This caused the exception “Context validation error for tag CFCASE. The CFSWITCH has a duplicate CFCASE for value "0.0".” The tag now returns the expected result.

OTHER TIPS

why not just use val() and then do the cfswitch??? Val() will strip all leading zeroes from the value:

<cfset a = "010">
<cfset b = val(a)>

<cfdump var="#a#">
<cfdump var="#b#">
<cfdump var="#b eq 10#">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top