9 Replies Latest reply on Oct 30, 2012 4:18 PM by chrome88-AsJSgC

    cfloop issue cf9 to cf10

    chrome88-AsJSgC

      i have a loop setup on an insert that was running fine on cf9, but on cf10 it is erroring with an "expressions error"

       

      code is:

       

      <cfloop index="x" from="1" to="#cc#" step="1">

      <cfif IsDefined("FORM.["Interest" & #x#]") AND FORM.["Interest" & #x#] is 1>

          1

            <cfelse>

            0

        </cfif>

        ,

        </cfloop>

       

      it is looping through a form field "Interest1, Interest2, Interest3", etc

      cc is the number of form fields

       

       

      any suggestions appreciated.

       

      thanks.

        • 1. Re: cfloop issue cf9 to cf10
          Dan Bracuk Level 5

          When I use array notation, I don't have a period between the structure and the opening square bracket.  Given that CF tends to be less forgiving of those sorts of imperfections as the versions get higher, that might be the problem.

          • 2. Re: cfloop issue cf9 to cf10
            chrome88-AsJSgC Level 1

            thanks, but isn't the period necessary to separate the input type from the name of the form, e.g. FORM.Interest1 vs FORMInterest1

             

            If written without the loop, it would be:

             

            <cfif IsDefined("FORM.Interest1") AND FORM.Interest1 is 1>

                1

                  <cfelse>

                  0

              </cfif>

              ,

            <cfif IsDefined("FORM.Interest2") AND FORM.Interest2 is 1>

                1

                  <cfelse>

                  0

              </cfif>

              ,

            <cfif IsDefined("FORM.Interest3") AND FORM.Interest3 is 1>

                1

                  <cfelse>

                  0

              </cfif>

              ,

            • 3. Re: cfloop issue cf9 to cf10
              WolfShade Level 4

              form["interest"&x] is, I believe, the proper notation.

               

              ^_^

              • 4. Re: cfloop issue cf9 to cf10
                Dan Bracuk Level 5

                Regarding, "thanks, but isn't the period necessary to separate the input type from the name of the form, e.g. FORM.Interest1 vs FORMInterest1'

                 

                Not when I do it. 

                • 5. Re: cfloop issue cf9 to cf10
                  Adam Cameron. Level 5

                  i have a loop setup on an insert that was running fine on cf9, but on cf10 it is erroring with an "expressions error"

                   

                  code is:

                   

                  <cfloop index="x" from="1" to="#cc#" step="1">

                  <cfif IsDefined("FORM.["Interest" & #x#]") AND FORM.["Interest" & #x#] is 1>

                   

                   

                  Sorry, but that code you specify there will not run on any version of ColdFusion.

                   

                  Firstly you have a syntax error in your variable referencing as others have pointed out.  This is invalid:

                  FORM.["Interest" & #x#]

                   

                  It needs to be this:

                  FORM["Interest" & #x#]

                   

                  (ie: without the dot).

                   

                  Secondly, isDefined() only works with dot-notation variable names, eg: form.interest1 etc. It cannot be used with associative array syntax or any other sort of notation.  so - syntax errors aside - this will not work:

                   

                  IsDefined("FORM.["Interest" & #x#]")

                   

                  (oh... a third error... if you're using double quotes as the string delimiter, and have embedded double quotes in the string, you need to escape them, eg "".  However that is neither here nor there in this situation, as the code is broken in two other ways anyhow.

                   

                  So you're giving us misleading information there.

                   

                  What you want to be using is this:

                   

                  structKeyExists(form, "Interest#x#") and form["Interest#x#"]

                   

                  Although using the & operator will work fine there too, instead of the inline variable resolution.

                   

                  Avoid isDefined() altogether if poss, as it's a bit old and creaky and gives false positives compared to using structKeyExists(), which just works.

                   

                  --

                  Adam

                  • 6. Re: cfloop issue cf9 to cf10
                    Adam Cameron. Level 5

                    Dan Bracuk wrote:

                     

                    Regarding, "thanks, but isn't the period necessary to separate the input type from the name of the form, e.g. FORM.Interest1 vs FORMInterest1'

                     

                    Not when I do it. 

                     

                    Nor when anyone else does it in the context the OP is using it.

                     

                    As you know, Dan: it's necessary when using dot notation (hence... err... the name "DOT notation"), but it's invalid when using associative array notation as we have here.  It's not "less than ideal" or "not the way I prefer doing it", it simply doesn't compile.

                     

                    --

                    Adam

                    • 7. Re: cfloop issue cf9 to cf10
                      Adam Cameron. Level 5

                      OFF TOPIC

                       

                      Hey Dan, whilst you're lurking... did you get my direct message the other week (last week?)?

                       

                      --

                      Adam

                      • 8. Re: cfloop issue cf9 to cf10
                        Dan Bracuk Level 5

                        I got an private message?  Hey Adam, did you get the reply I sent a couple of seconds ago?

                        • 9. Re: cfloop issue cf9 to cf10
                          chrome88-AsJSgC Level 1

                          Thanks for the help. Got it working now.