Check if a variable exists ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • jmoisan
    Forum Member
    • Jun 2002
    • 15

    Check if a variable exists ?

    Hi there,

    I was wondering if there was a way I could check whether or not a variable exists. For instance, in PHP, I'd write "IF EXIST (Variable)", but in AMS4, I can't do that.

    Any ideas ? Thanks in advance ! [img]/ubbthreads/images/icons/smile.gif[/img]
  • Derek
    Indigo Rose Customer
    • May 2001
    • 1254

    #2
    Re: Check if a variable exists ?

    Locally or Globally?
    Design Time or Run Time?
    -
    = Derek
    ["All glory comes from daring to begin" - fortune cookie]

    Comment

    • jmoisan
      Forum Member
      • Jun 2002
      • 15

      #3
      Re: Check if a variable exists ?

      Globally, run time.

      However, I found a workaround with the DoesKeyExist action, and it seems to work fine.

      Comment

      • Corey
        Indigo Rose Staff Alumni
        • Aug 2002
        • 9741

        #4
        Re: Check if a variable exists ?

        Actually in PHP you would use isset as in:

        if (isset($myVariable)) {
        doFunction();
        }

        or

        if (!isset($myVariable)) {
        doFunction();
        }

        But that's irrelevant...

        In AMS just use a conditional IF statement to check it's value and then execute on the result, i.e. if your variable equals zero then it isn't set...

        Corey Milner
        Creative Director, Indigo Rose Software

        Comment

        • Lorne
          Indigo Rose Staff Member
          • Feb 2001
          • 2729

          #5
          Re: Check if a variable exists ?

          A word of caution: if a variable in AMS 4.0 isn't set to anything, it is just plain text.

          In other words, until I assign some value to %myvar%, it's just that: a percentage character, the letter 'm', the letter 'y', the letter 'v', the letter 'a', the letter 'r', and another percentage character.

          Now, all strings in an expression are false, except for the word "true" (case insensitive). So you can't always just test for the variable to be true or false, because if it hasn't been set to anything, it will always be false.

          Of course, if you're only interested in doing something when the variable is true, then it's no problem performing a Boolean test on it like that...you can set things up so the "unset" condition just acts like having the variable set to "false" by default.

          An example of this is when you want to do something the first time a page is shown, and never again:

          <pre>IF( ! %FirstTimePage4Shown%)
          // do whatever

          // prevent this block from being performed again
          %FirstTimePage4Shown% = "1";
          END IF</pre>
          Now, if you really want to determine whether a variable has been set already, you can use a "String - Find" on the variable, and search for the name of the variable without the percentage signs. For example, to find out if %myvar% has been set yet, do a "String - Fing" to search for "myvar" in that variable. If the variable has been set to something, the Find action will return -1 (unless you assigned "myvar" to %myvar%, in which case you need to adjust your variable name [img]/ubbthreads/images/icons/wink.gif[/img]).
          --[[ Indigo Rose Software Developer ]]

          Comment

          Working...
          X