How should the local variable be called in if?

: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?

{
: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

: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~

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?

My main purpose is to assign a value to variable q, and changing it to: global doesn’t work either..

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?

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

{
: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