Skip to main content
Known Participant
December 29, 2009
Answered

cfloop over cfif

  • December 29, 2009
  • 2 replies
  • 556 views

I have a group of variables that I want to perform the same cfif statement for. I don't know the correct syntax for it and can't seem to find it anywhere. I know the code below doesn't work, but it shows what I'm trying to do.

How do I combine text and a variable together such as URL.#cookieName#?

<cfloop index="cookieName" list="myPro,myPub,myUni,myLes,peerPro,peerPub,peerUni,peerLes">

<cfif StructKeyExists(URL, "#cookieName#") AND URL.#cookieName# EQ "t" AND COOKIE.#cookieName# EQ "on">

   <cfset COOKIE.#cookieName# = "off">

   <cfelseif StructKeyExists(URL, "#cookieName#") AND URL.#cookieName# EQ "t" AND COOKIE.#cookieName# EQ "off">

   <cfset COOKIE.#cookieName# = "on">

</cfif>

</cfloop>

    This topic has been closed for replies.
    Correct answer BKBK

    <cfloop index="cookieName" list="myPro,myPub,myUni,myLes,peerPro,peerPub,peerUni,peerLes">

        <cfif StructKeyExists(URL, cookieName) AND URL[cookieName] EQ "t" AND COOKIE[cookieName] EQ "on">

           <cfset COOKIE[cookieName] = "off">

        <cfelseif StructKeyExists(URL, cookieName) AND  URL[cookieName] EQ "t" AND COOKIE[cookieName] EQ "off">

          <cfset COOKIE[cookieName] = "on">

        </cfif>   

    </cfloop>

    2 replies

    Inspiring
    January 5, 2010

    One of the "little secrets" of ColdFusion is that a "struct" is actually like what would be called a "hash" in other languages.  It supports a hybrid syntax, so that mystruct.foo and mystruct['foo'] are the same.  The very important difference in the second syntax-form is that the index can be specified using another variable or by any arbitrary expression, e.g. "bar = 'foo'; mystruct[bar]"

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    December 29, 2009

    <cfloop index="cookieName" list="myPro,myPub,myUni,myLes,peerPro,peerPub,peerUni,peerLes">

        <cfif StructKeyExists(URL, cookieName) AND URL[cookieName] EQ "t" AND COOKIE[cookieName] EQ "on">

           <cfset COOKIE[cookieName] = "off">

        <cfelseif StructKeyExists(URL, cookieName) AND  URL[cookieName] EQ "t" AND COOKIE[cookieName] EQ "off">

          <cfset COOKIE[cookieName] = "on">

        </cfif>   

    </cfloop>

    Known Participant
    December 29, 2009

    Thanks, that does the trick. I looked all over for how to write that and just couldn't find it.