Zach
Zach

Reputation: 21

ArrayContains undefined coldfusion

I am getting the error Variable ARRAYCONTAINS is undefined. I'm not sure what the problem is with my syntax.

Here's my code:

<cfoutput>
    <cfinvoke method="estimatedShipping" state="CA" returnvariable="days"/>
    CA: #days#
    <br>
</cfoutput>

<cffunction name="estimatedShipping" output="false" returntype="any">
    <cfargument name="state" type="string" required="yes"/>

    <cfset oneDayStates = ["CA"] >
    <cfset twoDayStates = ["WA","OR","NV","ID","AZ", "UT"] >
    <cfset threeDayStates = ["MT","WY","CO","NM"] >
    <cfset fourDayStates = ["ND","SD","NE","KS","OK","TX","MN","IA","MO","AR","LA", "WI","MI","IL","IN","KY","TN","MS","AK","AL","GA","OH","WV"] >
    <cfset fiveDayStates = ["ME","VT","NY","NH","MA","RI","CT","NJ","PA","MD","DE","VA","NC","SC","FL", "HI"] >

    <cfif #arrayContains( oneDayStates, state)# eq 'YES'>
        <cfset count = "1">
    <cfelseif #arrayContains( twoDayStates, state)# eq 'YES'>
        <cfset count = "2">
    <cfelseif #arrayContains( threeDayStates, state)# eq 'YES'>
        <cfset count = "3">
    <cfelseif #arrayContains( fourDayStates, state)# eq 'YES'>
        <cfset count = "4">
    <cfelseif #arrayContains( fiveDayStates, state)# eq 'YES'>
        <cfset count = "5">
    <cfelse>
        <cfset count = "5+">
    </cfif>
    <cfreturn count />
</cffunction>

Upvotes: 2

Views: 372

Answers (2)

Alex
Alex

Reputation: 7833

Here is a solution for ColdFusion 8 and below:

<cffunction name="estimatedShipping" output="false" returntype="any">
    <cfargument name="state" type="string" required="yes">

    <cfset var LOCAL = structNew()>

    <cfset LOCAL.oneDayStates   = ["CA"]>
    <cfset LOCAL.twoDayStates   = ["WA", "OR", "NV", "ID", "AZ", "UT"]>
    <cfset LOCAL.threeDayStates = ["MT", "WY", "CO", "NM"]>
    <cfset LOCAL.fourDayStates  = ["ND", "SD", "NE", "KS", "OK", "TX", "MN", "IA", "MO", "AR", "LA", "WI", "MI", "IL", "IN", "KY", "TN", "MS", "AK", "AL", "GA", "OH", "WV"]>
    <cfset LOCAL.fiveDayStates  = ["ME", "VT", "NY", "NH", "MA", "RI", "CT", "NJ", "PA", "MD", "DE", "VA", "NC", "SC", "FL", "HI"]>

    <cfset ARGUMENTS.state = uCase(ARGUMENTS.state)>

    <cfif LOCAL.oneDayStates.indexOf(ARGUMENTS.state) gte 0>
        <cfset LOCAL.count = "1">
    <cfelseif LOCAL.twoDayStates.indexOf(ARGUMENTS.state) gte 0>
        <cfset LOCAL.count = "2">
    <cfelseif LOCAL.threeDayStates.indexOf(ARGUMENTS.state) gte 0>
        <cfset LOCAL.count = "3">
    <cfelseif LOCAL.fourDayStates.indexOf(ARGUMENTS.state) gte 0>
        <cfset LOCAL.count = "4">
    <cfelseif LOCAL.fiveDayStates.indexOf(ARGUMENTS.state) gte 0>
        <cfset LOCAL.count = "5">
    <cfelse>
        <cfset LOCAL.count = "5+">
    </cfif>

    <cfreturn LOCAL.count>
</cffunction>

I also fixed the inconsistency and properly scoped your local variables.

Upvotes: 1

Leigh
Leigh

Reputation: 28873

Since it is a self-contained example, run it on http://trycf.com. It works with CF10 and Lucee. So there is nothing technically wrong with it. You are probably using an older version that does not support that function. Check your CF version:

<cfdump var="#server.coldfusion.productversion#">

For an alternative to this function, take a look at http://cflib.org. They may have something you can use as a replacement, or at least the basis for one, such as ArrayContains UDF. That said, normally you would store this type of information in a database. Assuming that is not an option, you might find it easier to use a structure. Use the number of shipping days as the "key", and a list of states as the "value".

<cfset shipping    = {} >
<cfset shipping[1] = "CA" >
<cfset shipping[2] = "WA,OR,NV,ID,AZ,UT"  >
<cfset shipping[3] = "MT,WY,CO,NM" >
<cfset shipping[4] = "ND,SD,NE,KS,OK,TX,MN,IA,MO,AR,LA,WI,MI,IL,IN,KY,TN,MS,AK,AL,GA,OH,WV" >
<cfset shipping[5] = "ME,VT,NY,NH,MA,RI,CT,NJ,PA,MD,DE,VA,NC,SC,FL,HI" >

<cfset result = "5+">
<cfloop collection="#shipping#" item="days">
    <cfif listFindNoCase( shipping[ days ], stateCodeToFind)>
        <cfset result = days >
        <cfbreak>
    </cfif>
</cfloop>

Nothing to do with the question, but

  1. Always var or local scope all function local variables
  2. No need for the excess pound signs in the CFIF
  3. Since ArrayContains returns a boolean value, it is simpler to write:

    <cfif arrayContains( oneDayStates, state)>
    

    Instead of

    <cfif arrayContains( oneDayStates, state) eq 'YES'>
    

Upvotes: 2

Related Questions