Unexpected results by using LOCALVARS

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Massimo
Newbie
Posts: 15
Joined: Sat Dec 28, 2019 5:16 pm

Unexpected results by using LOCALVARS

Post by Massimo » Wed Feb 09, 2022 9:53 am

Dear support,
I'm using the tool since a certain time and I really like it :-)
However, In my opinion is really a pity that there is no implementation of a FUNCTION feature to get back variables in a safe way.

Anyway, I use SRT feature with the LOCALVARS paradigm.
I have carefully read the definition of Local Scope for variables, but something is not yet clear to me. I still experiment unexpected behaviors with variable localization.

Can someone explain me what is wrong and how should I use correctly LOCALVARS?


Simple example:
==============

' Global variables
Let>var_a=1
Let>var_b=2
Let>var_c=3
Let>Var_d=4

Gosub>Level1

SRT>Level1
Let>LOCALVARS=1
Let>var_d=2
Let>var_a=100
GoSub>Level2
Let>LOCALVARS=1
' Puts back environment to scope 1
' Result is 3 as expected
Let>var_d=var_d+1
'Result=11 , why? , should be 101 that is var_a in level 1
'Instead var_a works with global scope
Let>var_a=var_a+1
END>Level1

SRT>Level2
Let>LOCALVARS=1
Gosub>Level3
Let>var_a=200
' result is 201 as expected
Let>var_a=var_a+1
END>Level2

SRT>Level3
Let>LOCALVARS=1
Let>var_d=300
Let>LOCALVARS=0
Let>var_a=10
' Put back to level 3
Let>LOCALVARS=1
' result is 301 as expected
Let>var_d=var_d+1
END>Level3

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Unexpected results by using LOCALVARS

Post by Grovkillen » Wed Feb 09, 2022 10:08 am

Code: Select all

SRT>Level1
Let>LOCALVARS=1
Let>var_d=2
Let>var_a=100
GoSub>Level2
Let>LOCALVARS=1
' Puts back environment to scope 1
' Result is 3 as expected
Let>var_d=var_d+1
'Result=11 , why? , should be 101 that is var_a in level 1
'Instead var_a works with global scope
Let>var_a=var_a+1
END>Level1
Let>LOCALVARS=1

Look in the beginning, you never set it to zero
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Massimo
Newbie
Posts: 15
Joined: Sat Dec 28, 2019 5:16 pm

Re: Unexpected results by using LOCALVARS

Post by Massimo » Wed Feb 09, 2022 11:51 am

Thanks for the reply.
But, sorry I cannot catch my error.
Could you be more explicit?
What do you mean I never never set it to zero?

Massimo

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Unexpected results by using LOCALVARS

Post by hagchr » Wed Feb 09, 2022 10:40 pm

In SRT Level3 you set LOCALVARS=0 and then directly set var_a=10.

Since LOCALVARS is turned off you effectively change var_a to 10 for all levels (if you step through the script, you see that var_a, for both level 0 and for level 1, change from 1 and 100, to 10 respectively).

User avatar
Massimo
Newbie
Posts: 15
Joined: Sat Dec 28, 2019 5:16 pm

Re: Unexpected results by using LOCALVARS

Post by Massimo » Thu Feb 10, 2022 8:18 am

yes, but the intent of my example in SRT level 3 is to change var_a of level 0 without affecting the other localized var_a in other levels. How to do this?

In my example var-a of level 2 is not affected, but var_a of level1 it is.

To be honest I have a lot of pain to understand the behavior of variable localization with MS.
It is a pity that there is no implementation of Functions to handle variable scope in a simpler way :-)

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Unexpected results by using LOCALVARS

Post by hagchr » Thu Feb 10, 2022 9:52 am

Hi,
In my example var-a of level 2 is not affected, but var_a of level1 it is.
In level 2, if you add a MDL>var_a you can see it shows 10 as well before you set it to 200.

I have had similar problems in the past to get it to work as I want, so what I do now (for non-simple cases), is to have unique variable names in each SRT, eg in your case L0_var_a , L1_var_a, and L2_var_a and then define where it should affect the global variable var_a.

User avatar
Massimo
Newbie
Posts: 15
Joined: Sat Dec 28, 2019 5:16 pm

Re: Unexpected results by using LOCALVARS

Post by Massimo » Thu Feb 10, 2022 12:39 pm

Thanks for your recommendation.
In fact is what I do also to prevent problems.

However I find that this is a week point MS.
I try to make the most possible reusable code.
So I develop routines and I include when necessary in my code.
Today I'm facing similar problems as explained in the example in a 5000 lines code program with different include sections. I'm quite frustrated to see that some time I spend more time to debug issues with localized variables than develop the code.

In my side, I would pay the double the license cost of MS if a better implementation of variables & function paradigm to pass back parameters would be developed :-)

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