Community discussions

MikroTik App
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

How should the local variable be called in if?

Sun Feb 04, 2024 11:23 am

:if ([:len [/file find name=1.txt ]] > 0) do={
:local q [/file get "1" contents]
} else={
/file print file=1
:delay 2
:local q 0
}

:log warning [$q] ;
This code cannot run as expected, and the value of variable q is empty outside?
May I ask how to rewrite it?
 
msatter
Forum Guru
Forum Guru
Posts: 2912
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: How should the local variable be called in if?

Sun Feb 04, 2024 12:33 pm

{
:if ([:len [/file find name=1.txt ]] > 0) do={
:local q [/file get "1" contents]
} else={
/file print file=1
:delay 2
:local q 0
}
:log warning [$q] ;
}
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Sun Feb 04, 2024 3:52 pm

{
:if ([:len [/file find name=1.txt ]] > 0) do={
:local q [/file get "1" contents]
} else={
/file print file=1
:delay 2
:local q 0
}
:log warning [$q] ;
}
It still doesn't work, my ROS is ROS_ Long term v6.49.10
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How should the local variable be called in if?

Sun Feb 04, 2024 4:05 pm

:log warning [$q]
should not be:
:log warning "$q"

[] execute cmd/function, but you have a string/num.
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 3:51 am

:log warning [$q]
should not be:
:log warning "$q"

[] execute cmd/function, but you have a string/num.
This is not the root cause, I just want to assign a value to the variable q and found that it is normal within IF! But it cannot be called outside of IF!!!? So use: log warning to see if the assignment is successful~
 
millenium7
Long time Member
Long time Member
Posts: 539
Joined: Wed Mar 16, 2016 6:12 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 4:01 am

Thats because you are using the 'local' type which confines variables to the scope they are created within (and scopes deeper inside) but cannot exist outside of that scope. Thus as you've found it doesn't exist outside of the 'if' statement

Use :global instead of :local or place the :log command in the same scope level (inside both of the IF statements where its set)
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 4:51 am

Thats because you are using the 'local' type which confines variables to the scope they are created within (and scopes deeper inside) but cannot exist outside of that scope. Thus as you've found it doesn't exist outside of the 'if' statement

Use :global instead of :local or place the :log command in the same scope level (inside both of the IF statements where its set)
{
:if ([:len [/file find name=1.txt ]] > 0) do={
:global q [/file get "1" contents]
} else={
/file print file=1
:delay 2
:global q 0
}
:log warning $q ;
}
I modified the above code but it still doesn't work. Is it a bug in ROS?
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 6:22 am

Thats because you are using the 'local' type which confines variables to the scope they are created within (and scopes deeper inside) but cannot exist outside of that scope. Thus as you've found it doesn't exist outside of the 'if' statement

Use :global instead of :local or place the :log command in the same scope level (inside both of the IF statements where its set)
My main purpose is to assign a value to variable q, and changing it to: global doesn't work either..
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3509
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How should the local variable be called in if?

Mon Feb 05, 2024 7:47 am

I modified the above code but it still doesn't work. Is it a bug in ROS?
No. The issue is the "1" in "/file get 1 content" (or, /file get "1" content, both same). What using a numeric (or string that is a number) in "get" does is tell it to get the file at index 1 from last /file/print. While using a filename in "/file get ..." works, not if the name is a numeric since that the primary logic in get.

I suspect being explicit like this would help:
:local q [/file get [find name=1.txt] contents]
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 9:45 am

I modified the above code but it still doesn't work. Is it a bug in ROS?
No. The issue is the "1" in "/file get 1 content" (or, /file get "1" content, both same). What using a numeric (or string that is a number) in "get" does is tell it to get the file at index 1 from last /file/print. While using a filename in "/file get ..." works, not if the name is a numeric since that the primary logic in get.

I suspect being explicit like this would help:
:local q [/file get [find name=1.txt] contents]
{
:local yys 134 ;
:local gsd 1936 ;
:if ([:len [/file find name=("Save-".$yys.$gsd.".txt") ] ] > 0) do={
:global q [/file get ("Save-".$yys.$gsd.".txt") contents]
} else={
/file print file=("Save-".$yys.$gsd.".txt")
:delay 2
:global q 0
}
:log warning $q ;
}
Sorry~No help! What I said is that it is possible to assign values within IF, but not outside IF? I have now rewritten the script without using 1. txt, but I still cannot assign a value to variable q because there are two situations: 1) when there is no file, a new file will be created and q will be assigned a value of 0; 2) when there is a file, a number will be read from the file and assigned to variable q. This variable is intended to be used in a loop in the underlying code, such as for p from=$q to=99999 do={}. Isn't this considered a bug in ROS? Is there a problem with the writing style?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12014
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How should the local variable be called in if?  [SOLVED]

Mon Feb 05, 2024 10:54 am

Instead of insisting with useless ";", understand that all variables, local and global, must be defined at the start of the script, not on subsection.

untested example code

{
:local q 0
    :local yys 134
    :local gsd 1936
    :if ([:len [/file find where name="Save$yys$"gsd".txt"] ] > 0) do={
        :set q [:tonum [/file get "Save$yys$"gsd".txt" contents] ]
    } else={
        /file print file="Save$yys$"gsd".txt"
        :delay 2s
#        and why here the content of the file not set to 0
#        :local q 0 is useless, is already defined 0 at the start
    }
:log warning $q
}
 
rosa
newbie
Topic Author
Posts: 41
Joined: Mon Jan 15, 2024 6:10 am

Re: How should the local variable be called in if?

Mon Feb 05, 2024 11:46 am

Instead of insisting with useless ";", understand that all variables, local and global, must be defined at the start of the script, not on subsection.

untested example code

{
:local q 0
    :local yys 134
    :local gsd 1936
    :if ([:len [/file find where name="Save$yys$"gsd".txt"] ] > 0) do={
        :set q [:tonum [/file get "Save$yys$"gsd".txt" contents] ]
    } else={
        /file print file="Save$yys$"gsd".txt"
        :delay 2s
#        and why here the content of the file not set to 0
#        :local q 0 is useless, is already defined 0 at the start
    }
:log warning $q
}
Oh! Great God!! I succeeded with your method! It seems that if IF uses: local internally, attention should be paid. Thank you very much~Thank you very much

Who is online

Users browsing this forum: No registered users and 9 guests