Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

array notation strangeness

Participant ,
Jun 26, 2008 Jun 26, 2008
In the cfoutput they all give the same result: 1.
I understand why using dot notation for the first example mystruct.key1 will give me the key1 of the mystruct structure and why using array notation mystruct["key1"] will give me the result 1 as key1 in the mystruct structure is 1 but I don“t understand why the #mystruct[key1var]# gives me 1 as well. As far as I can gather it should give me the result key1 as a string as key1var is a variable with the string value key1. It must be because it copies the key1 value from the structure but I have no idea why. Why is this please? Thank you

TOPICS
Getting started
399
Translate
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 , Jun 26, 2008 Jun 26, 2008
Hydrowizard wrote:
> Why does this happen though and how does coldfusion evaluate it? TIA

I'm not sure what your difficulty is, but but my interpretation is that
you are confused by ColdFusion evaluating the variable 'key1var' to the
value 'key1' and then using that value as the key of the structure.

This is expected, accepted, designed and desired behavior. Otherwise
how would one dynamically call structure elements using variables if CF
did not evaluate those variables.

Maybe this basic...
Translate
Participant ,
Jun 26, 2008 Jun 26, 2008
I have worked why it get the result, it seems that the mystruct[key1var] is evaluated using the string so the output is actually mystruct[key1] whether that is with or without the " " I don't know but it evaluates it the same as mystruct.key1 or mystruct["key1"] Why does this happen though and how does coldfusion evaluate it? TIA
Translate
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
Jun 26, 2008 Jun 26, 2008
The [ ] is telling ColdFusion to evaluate what is inside the brackets and find the key for the structure. This is normal and correct behavior.

This allows you to set the key value dynamically in the code and have a very flexible program. You may not always know what the key is before the code runs. This is a very powerful feature that is hard to do in some programming languages.

I use it for an application that has user built forms that automatically create themselves at runtime and I have no idea ahead of time how many fields there are or what the key names will be.
Translate
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 ,
Jun 26, 2008 Jun 26, 2008
Hydrowizard wrote:
> Why does this happen though and how does coldfusion evaluate it? TIA

I'm not sure what your difficulty is, but but my interpretation is that
you are confused by ColdFusion evaluating the variable 'key1var' to the
value 'key1' and then using that value as the key of the structure.

This is expected, accepted, designed and desired behavior. Otherwise
how would one dynamically call structure elements using variables if CF
did not evaluate those variables.

Maybe this basic example would make the point a little clear.

<cfset aStruct = structNew()>
<cfset aStruct.key1 = "Foo">
<cfset aStruct.key2 = "Bar">
<cfset aStruct.key3 = "George">

<cfoutput>
<cfloop list="key1,key2,key3" index="j">
#aStruct#
</cfloop>
<hr/>
<cfloop from="1" to="3" index="j">
#aStruct['key' & j]#
</cfloop>
</cfoutput>

This technique is very powerful when combined with the knowledge that
form data is a structure.
Translate
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
Participant ,
Jun 26, 2008 Jun 26, 2008
LATEST
thanks for those replies guys- if the code was <cfset key1Var= key1> then that would make sense straight away as it is obviously referencing a variable. The fact that it is a string spun me out as I couldn't understand why that would be evaluated. Thanks for those explanations. A VERY important point to understand I hope other newbies can see this post aswell as it is THE KEY (no pun intended!) to really getting into dynamic cf!!


Translate
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