Context
For my model I would like to have an input where the user can enter a series of values.
E.g.
What I would like to have from the input shown above is a list of five numbers, e.g. [0.5 0.2 0 0.2 0.5]
, so I can then use the numbers that they enter for some calculations.
The problem
Unfortunately, having an input set up like above will spit out "0.5 0.2 0 0.2 0.5"
if I set the type to be a string. If I set the type to numeric, it will only allow a single number to be entered.
So, how can I parse the string the basis of a space (i.e. " ")? I am open to alternatives too, although I would prefer to keep it in Netlogo (e.g. not reading in a text file of values) to make it easier to change, as it is something that I suspect will get played around with a lot.
What I have tried
I have tried using read-from-string
, but it also doesn't like a series of numbers entered like above. I also attempted to use the explode
function from the string extension (https://github.com/NetLogo/String-Extension), but my version of Netlogo (6.2.0) did not like the API from that extension and wouldn't allow me to use it.
I am very new to NetLogo, so sorry if my question is silly or I have not made something clear!
As per the docs on it, read-from-string
can parse a list of literal values. The issue you are having is that a NetLogo list literal must have square brackets to open and close, as per the Constant Lists section of the Programming Guide. So all you need to do is add [
and ]
to your user's input.
to test
let s "0.5 0.2 0 0.2 0.5"
let l read-from-string (word "[" s "]")
show l
show item 2 l
end
Output:
observer> test
observer: [0.5 0.2 0 0.2 0.5]
observer: 0
I would caution, though, that it would be very easy for users to enter numbers with a different format, like 0, 2, 3, 5.0
, using commas to separate values. A check that the conversion actually worked would be wise, as the error message you'd get from the failed read-from-string
probably wouldn't be helpful to the model user.