[thelist] nesting structures in ColdFusion
Steve Lewis
nepolon at worlddomination.net
Thu Jul 12 20:45:59 CDT 2001
I am a php/mysql weenie who is learning ColdFusion now, and I made the
mistake of assuming that nested structures could be interated through
using cfloop the way you can in most other languages I know...
[begin broken example]
<cfset mystruct = StructNew()>
<cfset mystruct.sub1 = StructNew()>
<cfset mystruct.sub1.name = "name_1">
<cfset mystruct.sub1.favcolor = "color_1">
<cfset mystruct.sub2 = StructNew()>
<cfset mystruct.sub2.name = "name_2">
<cfset mystruct.sub2.favcolor = "color_2">
<cfloop collection="#mystruct#" item="curritem">
<cfoutput>
#curritem.name#'s favorite color is #curritem.favcolor#<br>
</cfoutput>
</cfloop>
[end broken example]
This does not work because #curritem# is a string containing the value of
the key, not the value itself, and not a pointer to the value, and not an
object reference... three things I am accostomed to dealing with. Why
does it treat curritem as a string? I dont like it, but dem's tuff luck.
So with the patient help of an experienced ColdFusion user, I find myself
looking at the following:
[begin working example]
<cfloop collection="#mystruct#" item="curritem">
<cfset thecollection = 'mystruct.'&curritem>
<cfloop collection="#Evaluate(thecollection)#" item="newitem">
<cfoutput>#thecollection#.#newitem#
=#Evaluate(thecollection&'.'&newitem)#</cfoutput><br>
</cfloop><br><br>
</cfloop>
[end working example]
Now my problem is that this feels like a *hack* to me as I have personal
issues with the evaluate() function... in fairness I consider the PHP
equivalent to be perfectly fine. (It's just a matter of taste over
language semantics.)
My challenge:
can anyone show me how to do the above more "elegantly" or whatever?
thanks,
--Steve
More information about the thelist
mailing list