Netlogo: Call commands from text files? - netlogo

Is it possible to call commands from a text file?
The goal I am trying to achieve is to have a text file with strings of commands ,the user can input this file in and the system will then call the commands.
What I have so far is from studying the file input example is:
to load-test
let file user-file
file-open file
let lines []
while [not file-at-end?]
[
let a-line file-read
set lines lput a-line lines
]
file-close
end
The list lines will contain all of the lines of the file, Then use a foreach on the list to select each element at a time to execute.
I am aware of the primitive "read-from-string" but it only seems to work from values rather than commands.
Is there any method of achieving this or something similar?

If the command is a reporter, you can use runresult, otherwise, you could use run
See: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#run
For example:
show runresult "3 + 2"
It's a little weird that you're storing commands in a text file, why not just use an nls file to store the extra commands?

Related

Is there a way to execute a text string as a command?

In NetLogo is there a way to treat a text string as a command and execute it?
In the simplest case - is there way to simply generate a text string like "forward " concatenated with a number I don't know till runtime, so i end up with a text variable equal to "forward 8", say, and then execute that command?
My immediate need is probably what LOGO did, namely, have the user click buttons to move a turtle around with the pen down to draw some shape, say a square, saving those commands as they are written to an output window, saving the full text of the output window to some named text like "draw-square", and then have the user able to type "draw-square" in the command center and have the turtle execute those steps and draw a new square.
I could work around this I suppose by exporting the output to a myfile.nls text file, then restarting NetLogo to bring in the new command file using the __includes [ "myfile.nls"] step , but I'd rather not ask them to do that if I can avoid it and in any case that wouldn't work over the web.
Jasper answered the question in the comment above -- "run" and "runresult" do what I wanted.

how to set a string in netlogo ? how to add charachters to a string in netlogo

i was coding with netlogo 3.1.5 and i wrote the next code and it works (the S was already declared in globals [])
set S ""
ifelse (color = green)
[set S S + "0"][set S S + "1"]
the problem is when i wanted to work in netlogo 6.1.1 i copied and past the same code and it shows me an error which is the following:
+expected this input to be a number, but got a string instead
so can you please tell me how to set a string and add other strings in it ?
I wasn't using NetLogo back at v3 but I'm assuming the + is to concatenate. If so, the primitive you want is word. Here's a complete model for demonstration. Note that you need the bracket version if there are more than two inputs.
to testme
let S "A"
print S
set S word S "B"
print S
set S (word S 1 2)
print S
end

Is there a way to insert N times the same characters

I can't find if vscode has a such failure. Is there a way to construct a string with N characters?
I explain myselft:
I need to wrote an empty string like this:
foobar = "1111111111111111";
There is 16 times characters '1'. Is there a way, like in Vim, to construct the line like this:
i wrote 'foobar = "' then i'd make a command to repeat 16 times the character 'i'.
I hope it's clear for you.
Here is an easy way using HyperSnips - a snippet extension that can use javascript to produce the output. First the demo:
The HyperSnips snippet:
snippet `"(.+)\*(\d+)=` "expand" A
``
let origStr = m[1];
let howMany = parseInt(m[2]);
let newStr = origStr.repeat(howMany);
rv=`"${newStr}`
``
endsnippet
This code goes inside a <yourLanguage>.hsnips file to have it run only in that language or all.hsnips to run in all files.
I made it to run inside a "" using this key: (.+)\*(\d+)=
The = is really the trigger - it runs automatically - you could change that to be something else. [The key could be shorter if you didn't care about digits being repeated.]
For more on setting up HyperSnips (which is pretty easy) see VSCode Advanced Custom Snippets
There currently is no native way, outside of copy/pasting.
You can use Repeat Paste:
Copies selected text and pastes repeatedly based on user input. Functions like Vim yank, paste, and repeat. For example, in Vim you would select the characters you want to copy then type 30p to paste the selected text 30 times.
Select a char and activate your command palette with CTRL + SHIFT + P and type 'Repeat Paste' and it will prompt you for the quantity.
You can assign a shortcut to this command
You can use the extension Regex Text Generator. You write a Regular Expression that generates your needed text
Type the following in the Generate Box
foobar = "1{16}";

Prevent typing more than one space in VSCode with the exception of the start of a line

I want to restrict more than one space to be typed in VS Code unless it is before the first non-space in a line.
Additionally, if you open a file that was like that already I want those multi-spaces to be removed when saved.
Here is Trailing Spaces - it looks like a workaround for what you're looking for.
You are really looking for a formatter for these two functionalities: format on type and format on save. However, you can get the first function - "restrict more than one space to be typed in Visual Studio Code unless it is before the first non-space in a line" with the help of an extension HyperSnips.
For setting up that extension, see https://stackoverflow.com/a/62562886/836330
Then say in your all.hsnips file put this:
snippet `(\S)( ){2,}` "found 2 consecutive spaces with no non-whitespace characters preceding" A
``rv = m[1] + " "``
endsnippet
That will work in all language files, you can restrict it to certain languages as mentioned in that answer linked to.
Run the command HyperSnips: Reload Snippets.
Basically you are running a snippet that uses (\S)( ){2,} as the prefix. It will be triggered whenever it detects two or more spaces in a row and will replace them with just one!
The snippet will only be triggered if those two spaces are preceded by a non-whitespace (so some character) and thus your second requirement that multiple spaces be allowed before the first character in a line.
It also works between existing words if you go there to edit - you will not be able to add a space to an existing space!!
Demo:
The gif doesn't really capture it well but every time I am trying to write a second consecutive space it deletes it leaving only the one.
Your second question was "if you open a file that was like that already I want those multispaces to be removed when saved".
I suggest running a script on save. Using, for example, Trigger on Task Save. In your settings:
"triggerTaskOnSave.tasks": {
"stripSpaces": [
"**/*.txt" // here restricting it to .txt files
],
},
This runs the stripSpaces task whener you save a .txt file. That task (in tasks.json) could look like:
{
"label": "stripSpaces",
"type": "shell",
"command": "awk -f stripSpaces.awk ${relativeFile} > temp; mv temp ${relativeFile}"
}
which runs this stripSpaces.awk script:
# strip multiple consecutive spaces from a file
# unless they are the beginning of the line
BEGIN {
regex="\\S( ){2,}";
}
{
if (match($0,regex)) {
gsub(/( ){2,}/, " ")
print
}
else print
}
Demo:

How to use input box to enter formula?

I am using NetLog to test learning curve formula. I would like to use the input box to interactively enter something as simple as
current-tick * 23
No joy. I can capture a current ticks value in a set command, but I cannot get the input box to accept a formula. Once I learn how to do it, I can work out how to do larger formula.
set knowledge-increment []
set knowledge-increment + learning-equation * 25
learning-equation is the label for the input box.
Any suggestions?
The input box sets the value of the global variable specified. It is a string. To use that string in code, something like this will work
run (word "show " learning-equation)
or more to your point of setting a value
globals [new-equ]
to go
run (word "set new-equ " learning-equation)
show (word "new-equ is " new-equ)
end
I got it working in basic form. Now to apply it. The input box called increment, uses a no quotes equation.
I used stuff as a global.
The code line contains the set command within the quotes. Now I can use stuff as a constant as in:
run (word “set stuff ” increment)
show stuff * ticks
I just have to avoid using global variables such as ticks in a turtle setting.
Thank you Lon and Andrew