[thelist] CF: Persistent variables from a loop. How?

jeff jeff at members.evolt.org
Wed May 9 20:59:42 CDT 2001


frank,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Frank
:
: Can someone suggest a way whereby I can
: use a loop to create a persistent variable?
: Here is an example to illustrate what I'm
: trying to accomplish:
:
: <!--- Define a list of fields in a form --->
:     <cfset TheList="Name,Email,Address">
:
: <!--- Set the defaults --->
:     <cfloop index="TheList">
:        <cfparam name="Form.#index#" default="">
:     </cfloop>
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

probably not in your working copy, but your <cfloop> has an error.  you have
an index attribute, but you're not telling it if it's a list loop, a
for/next loop or what.

<cfloop list="#thelist#" index="element">
  <cfparam name="form.#element#" default="">
</cfloop>

fwiw, i think that using hashed variable and the dot syntax is dirty (you
can't access a scoped variable in this fashion, why should you be able to
set it in this fashion) and hard to read.  any good programmer knows that
you shouldn't have an unknown in the declaration of a variable (ie, a
dynamically generated portion of a variable name on the left side of the
assignment operator (=)).  if you absolutely have to create the variables
this way, then may i suggest you use bracket notation instead.

  <cfparam name="form[#element#]" default="">

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: As it stands, obviously, the only one that persists
: is the last, since each iteration destroys the
: previous one. So, what would be a good way of making
: them all stick?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

how do you figure each successive one is destroying the previous?  as long
as each value in the list is unique you will create an instance of a form
scoped variable for each item in the list.

that aside, i don't see any real benefit for maintenance or readability
reasons to go to this trouble.  by separating the list of variables from the
<cfparam> you're using to set up the defaults you've removed clear
indication of how the variables in your list are scoped.  furthermore, your
eyes are far more suited to scanning a column of words than they are suited
to breaking up a comma-delimited list into separate words -- especially when
the variable names will all align themselves magically making it even more
obvious.

<cfparam name="form.name" default="">
<cfparam name="form.email" default="">
<cfparam name="form.address" default="">

and finally, you've introduced another layer of processing just to param
some variables.

i'd recommend just a plain old grouping of <cfparam> tags for what you're
trying to do.

good luck,

Jeff Howden
Sr. Web Application Engineer
jeff at alphashop.net

AlphaShop Network Services
http://www.alphashop.net/
Mobile: 503.804.9938
Voice:  541.681.4078
Fax:    541.681.4084

AIM - Active Information Management





More information about the thelist mailing list