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