• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfloop issue cf9 to cf10

Guest
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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.

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 30, 2012 Oct 30, 2012

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

...

Votes

Translate

Translate
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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>

  ,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

OFF TOPIC

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

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 30, 2012 Oct 30, 2012

Copy link to clipboard

Copied

LATEST

Thanks for the help. Got it working now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation