How to use input box to enter formula? - netlogo

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

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.

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}";

how to add different number at end of multi line edit?

Having trouble finding a way to do this, maybe it is not even possible?
In my case, for testing flow of if-statements/user-interaction, am temporarily adding 40 lines of console.log('trigger-fired-1'); throughout our code.
However, to tell them apart would like each to end with a different number, so in this case, numbers one to forty like so:
In the screen recorded gif, to replicate what I am going for, all I did was copy/paste the numbers one to nine. What I really would like is a shortcut key to generate those numbers at the end for me to eliminate that step of typing out each unique number.
Am primarily coding in Visual Studio Code or Sublime Text, and in some cases shortcuts are similar, or at least have same support but for different shortcut keys.
There are a few extensions that allow you to do this:
Text Pastry
Increment Selection
NumberMonger
For Sublime Text, the solution to this problem is the internal Arithmetic command. Something similar may or may not be available in VS Code (possibly with an extension of some sort) but I'm not familiar enough with it to say for sure.
This command allows you to provide an expression of some sort to apply to all of the cursor locations and/or selected text.
By way of demonstration, here's the example you outlined above:
The expression you provide is evaluated once for every selection/caret in the buffer at the time, and the result of the expression is inserted into the buffer (or in the case of selected text, it replaces the selection). Note also that when you invoke this command from the input panel (as in the screen recording) the panel shows you a preview of what the expression output is going to be.
The special variable i references the selection number; selections are numbered starting at 0, so the expression i + 1 has the effect of inserting the selection numbers starting at 1 instead of 0.
The special variable x refers to the text in a particular selection instead. That allows you to select some text and then transform it based on your expression. An example would be to use x * 2 immediately after the above example (make sure all of the selections are still present and wrapping the numbers) to double everything.
You can use both variables at once if you like, as well as anything in the Python math library, for example math.sqrt(i) if you want some really esoteric logs.
The example above shows the command being selected from the command palette interactively, where the expression automatically defaults to the one that you want for your example (i + 1).
If you want to have this as a key binding, you can bind a key to the arithmetic command and provide the expression directly. For example:
{
"keys": ["super+a"],
"command": "arithmetic",
"args": {
"expr": "i+1"
},
},
Try this one ...
its not like sublime
but works g
https://github.com/kuone314/VSCodeExtensionInsertSequence

Netlogo: Call commands from text files?

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?

How to prompt for input using an existing variable in the prompt

I'm trying to ask the user for a value of some variable, but at the same time, showing him the last value used (at the termination of the program, the values are saved to a file, and loaded at the start of the program).
Something like this:
Enter new radius value (R=12.6) :
... user enters 12.7 ...
Enter new pi value (pi=3.14) :
Enter new height value (h=30.0) :
Usually I would write the first one with write statement, then read the new one (in Fortran, for example). In MATLAB however, I don't know how to write something out with input statement. Are there some other statements for getting input ?
You can use the command input for this, combined with sprintf.
%# set defaults
radius = 12.6;
%# ask for inputs
tmp = input(sprintf('Enter new radius value (R=%4.2f)\n',radius));
%# if the user hits 'return' without writing anything, tmp is empty and the default is used
if ~isempty(tmp)
radius = tmp;
end
As an alternative, you may want to look into INPUTDLG