3

I'm trying to determine if a variable with a variable name is defined. Please help with my syntax... my attempts so far:

<cfif isDefined(Evaluate("session['#url.sessionSQL#']['SQL_ALL']"))>

<cfif isDefined('Evaluate("session[#url.sessionSQL#]")["SQL_ALL"]')>

<cfif isDefined(Evaluate("session['#url.sessionSQL#']['SQL_ALL']"))>        

<cfif isDefined('session[Evaluate("#url.sessionSQL#")]["SQL_ALL"]')>

<cfif isDefined('session["#url.sessionSQL#"]["SQL_ALL"]')>

Thanks.

earachefl
  • 1,880
  • 7
  • 31
  • 55

4 Answers4

6

Your question is a little confusing ;)

If session[url.sessionSQL]['SQL_ALL'] contains the name of a variable, you can use structKeyExists to verify that variable exists in a particular scope.

<cfif structKeyExists(scopeToCheck, "TestForThisVariableName")>  
... ie
<cfif structKeyExists(variables, session[url.sessionSQL].SQL_ALL)>

On the other hand, if just want to verify those session variables exist

  <cfif structKeyExists(session, url.sessionSQL) AND 
        structKeyExists(session[url.sessionSQL], "SQL_ALL")>

Either way, you do not need the evaluate() function.

Update: From comments, a key difference between IsDefined and StructKeyExists is precision. IsDefined examines a whole list of scopes when deteriming if a variable exists. Usually (though not always) that is undesirable because it can lead to unexpected results if you forget a particular variable exists in multiple scopes. (Using IsDefined inside a function is a prime example.) When you specifically want to check multiple scopes, then IsDefined() is more appropriate. Otherwise, I would stick with StructKeyExists as its results are less ambiguous.

Leigh
  • 28,765
  • 10
  • 55
  • 103
  • While Henry's answer isn't wrong, voting Leigh's up, as I prefer the structKeyExists() route vs. isDefined(). – charliegriefer Oct 21 '11 at 18:00
  • `` would not work if the name of the variable is `"form.x`, or simply `"x"` which the programmer doesn't care what scope `x` is defined. Therefore, `IsDefined()` is still better. Performance difference is neglectable and correctness is more important. – Henry Oct 21 '11 at 18:31
  • 1
    @Henry - In terms of potential correctness, IsDefined() is actually the *least* accurate. If you disregard scope, there is no guarantee you are even verifying the correct `x` variable. As for the rest `variables` was an example. That is why I specified `scopeToCheck`. – Leigh Oct 21 '11 at 19:02
0
<cfif isDefined("#session[url.sessionSQL].SQL_ALL#")>

update

Assuming the name of the variable you want to check is stored in session[url.sessionSQL].SQL_ALL, but the pieces may not have been defined in url nor session nor in SQL_ALL

Then the safest version:

<cfif isDefined("url.sessionSQL") 
      and isDefined("session[url.sessionSQL]")
      and isDefined("session[url.sessionSQL].SQL_ALL")
      and isDefined("#session[url.sessionSQL].SQL_ALL#")>
Henry
  • 32,689
  • 19
  • 120
  • 221
  • 2
    The way Henry point out is the best way to handle that. There is rarely a good reason to use evaluate() anymore. – Jason Dean Oct 21 '11 at 17:53
  • Hm... tried this and couldn't make it work. In case I wasn't clear in my example, both #url.sessionSQL# and #session# are variables, while 'SQL_ALL' isn't. – earachefl Oct 21 '11 at 18:34
  • maybe check if `url.sessionSQL` is defined, and if `session[url.sessionSQL]` is defined, and if `session[url.sessionSQL].SQL_ALL` is defined first? – Henry Oct 21 '11 at 18:37
0

Even though the answer below from Henry is best, I will point out the problem with your syntax above.

You want the string inside of isDefined() to be evaluated to a string but it is in quotes (which of course is required for isDefined() because it expects the name of a variable, not an actual variable. So you need hashmarks to make evaluate() run within the quotes.

<cfif isDefined('#Evaluate("session[url.sessionSQL]")#["SQL_ALL"]')>

Otherwise it is looking for a variable named "session[#url.sessionSQL#]")["SQL_ALL"]"

I did not actually test this, but I believe this should work. But clearly, the other way is MUCH better.

Jason Dean
  • 9,585
  • 27
  • 36
0

EDIT:

As it turns out, Leigh's answer above works just fine, but not Henry's. In the interim, I found my own solution, as follows.

In the original question, I had a struct

"#session#"

which had an element referenced by a variable

"#url.sessionSQL#"

in other words,

"#session[sessionSQL]#" 

and I was trying to find whether that element had a structKey defined, named "SQL_ALL". I was able to make things work like so:

<cfset sessionSQL = #session[url.sessionSQL]# />
<cfif structKeyExists(sessionSQL, "SQL_ALL")>
earachefl
  • 1,880
  • 7
  • 31
  • 55
  • Just curious, how do the results differ from the suggested examples? Keep in mind you can always post a small dump of the data. It often helps cut to the chase. Especially with dynamic variables ... – Leigh Oct 24 '11 at 17:59
  • Well, turns out I'm partially wrong... Leigh, your answer was correct, while Henry's solution didn't work. Not sure if I copied your solution incorrectly. Anyway, my apologies, and I'm voting your answer up! – earachefl Oct 24 '11 at 18:23