SRT to set a variable to default if not already set

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
KTT
Newbie
Posts: 13
Joined: Thu Jul 02, 2009 10:26 pm

SRT to set a variable to default if not already set

Post by KTT » Tue Jun 15, 2010 5:35 am

Hi all,

I'm starting to get into Macro Scheduler programming, so I'm not quite sure what's possible and not.

I'm finding that I need to check if a variable is defined, and set it to default value if undefined. For example, test the TIMEOUT variable as such:

Code: Select all

    Assigned>TIMEOUT,result
    If>{%result%="FALSE"}
        Let>TIMEOUT=0
    EndIf
    // From here on, I can use TIMEOUT variable safely
This works. However, it would be nice to make this into a SRT for clean application, since it's done often. Ideally, the SRT call would be something like:

Code: Select all

GoSub>SetIfNotDefined,{"TIMEOUT"},0
I went so far as to code the SRT below:

Code: Select all

SRT>SetIfNotDefined
    // Save LOCALVARS to restore at exit to minimize side effect
    Let>TMP_LOCALVARS=%LOCALVARS%
    Let>LOCALVARS=0

    // SOWriteLn>%SetIfNotDefined_var_1% %SetIfNotDefine_var_2%

    // I can't get this statement to work correctly
    Assigned>%SetIfNotDefined_var_1%,result

    If>{%result%="FALSE"}
      SOWriteLn>%SetIfNotDefined_var_1% not defined, setting to %SetIfNotDefined_var_2%

      // This statement works as expected because LOCALVARS=0
      Let>%SetIfNotDefined_var_1%=%SetIfNotDefined_var_2%
    EndIf

    // Restore LOCALVARS
    Let>LOCALVARS=%TMP_LOCALVARS%
END>SetIfNotDefined
However, the Assigned statement in the SRT won't take a variable to check if the value in the variable is defined as variable, so the SRT above doesn't work correctly.

There is an existing enhancement request to allow Assigned to work for complex expressions: http://www.mjtnet.com/usergroup/viewtopic.php?p=25410

Until the above enhancement request is (hopefully) implemented, is it possible to implement the SRT above in a different way?

Thanks

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jun 15, 2010 1:33 pm

Try this instead of using Assigned>

Code: Select all

//Let>TIMEOUT=1

GoSub>SetIfNotDefined,TIMEOUT,0,{"TIMEOUT"}
MDL>result

SRT>SetIfNotDefined
    If>SetIfNotDefined_var_1=SetIfNotDefined_var_3
      Let>result=NOT DEFINED
    Else
      Let>result=DEFINED
    EndIf
END>SetIfNotDefined

KTT
Newbie
Posts: 13
Joined: Thu Jul 02, 2009 10:26 pm

Post by KTT » Tue Jun 15, 2010 9:57 pm

Beautiful! Thank you, JRL.

Here's the SRT call and declaration packaged up nicely.

Code: Select all

// This sequence sets the variable TIMEOUT to 0 if it's not already set
    Assigned>TIMEOUT,result
    If>{%result%="FALSE"}
        Let>TIMEOUT=0
    EndIf

// This call does the same thing as above with SRT SetIfNotDefined
GoSub>SetIfNotDefined,TIMEOUT,0,{"TIMEOUT"}


// SRT declaration
SRT>SetIfNotDefined
    // Save LOCALVARS to restore at exit to minimize side effect
    Let>TMP_LOCALVARS=%LOCALVARS%
    Let>LOCALVARS=0

    If>{%SetIfNotDefined_var_1%=%SetIfNotDefined_var_3%}
        // This statement works as expected because LOCALVARS=0
        Let>%SetIfNotDefined_var_1%=%SetIfNotDefined_var_2%
    EndIf

    // Restore LOCALVARS
    Let>LOCALVARS=%TMP_LOCALVARS%
END>SetIfNotDefined


User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jun 15, 2010 10:15 pm

You're welcome.

There is a catch... (isn't there always?)

If the variable HAS a value and that value is the test text, then a false negative will be detected. In other words, the sample subroutine will fail if the variable TIMEOUT has been set to the text "TIMEOUT".

e.g.
Let>TIMEOUT=TIMEOUT

KTT
Newbie
Posts: 13
Joined: Thu Jul 02, 2009 10:26 pm

Post by KTT » Wed Jun 16, 2010 4:28 am

JRL wrote:If the variable HAS a value and that value is the test text, then a false negative will be detected. In other words, the sample subroutine will fail if the variable TIMEOUT has been set to the text "TIMEOUT".

e.g.
Let>TIMEOUT=TIMEOUT
Understood. I think this is acceptable until this Assigned workaround is no longer needed.

Thanks.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts