Why does this code not work with a variable?

This does not work

:local ssids [:toarray [:deserialize [/file/get ssids.txt  contents] delimiter="\n" from=dsv options=dsv.plain]];

:foreach line in=$ssids do={:put $line}

This works

:foreach line in=[:toarray [:deserialize [/file/get ssids.txt  contents] delimiter="\n" from=dsv options=dsv.plain]] do={:put $line}

The code is exactly the same, except one uses a variable. What is going on? I spent 2 hours trying to read a file and list line by line and found out variables are not working at all?

ssids.txt

test
test2

Are you trying to run the lines directly in the Terminal? If yes then each line has its own scope, the variable $ssids you declared on the first line no longer exists on the 2nd line.

If running from the Terminal command line, put both lines in a { ... } block. After the opening { you can put as many lines as you want, and close with a final }. The block will then be treated as one single scope.

Alternatively, create a script under /system script then use /system script run to run it.

1 Like

Maybe if you'd spent those two hours reading the documentation, however poor it may be,
you'd surely have figured out the problem yourself... [as @CGGXANNX wrote]

:warning: Each line written in the terminal is treated as local scope

Anyone would expect variables, to, well, do their thing by default when creating them you know.

Well, there is a difference between local and global variables. If you'd use global in first example its scope, it would have worked.

As @rextended points out, local variable scope defined to the current CLI line:

So the { } block is what create a new scope, but that is still part of the same CLI line.

Now local scope is valid through out a /system/script (or scheduler). So to test a script at CLI, you do need the { } to emulate same scoping as /system/script would use.