Netlogo: Read-from-string expects literal value - list

The following triggers an "expected literal value" warning, what am I missing? I would expect the value 1000 to be returned.
let item-cost ["dia-cost"]
let item-cost first item-cost
print read-from-string item-cost

As you said in the comments, dia-cost is a variable with the value of 1000 that you're looking to get out. The core of the issue is that read-from-string only reads literal values of the type number, list, string, boolean, or nobody. The wording there is a bit confusing, as it says, "Interprets the given string as if it had been typed in the Command Center", so I could see how it'd seem like a variable should give its value with read-from-string just as you'd see in the Command Center, but variables are not literal values of those limited types.
Fortunately there is an easy alternative, you can use the runresult primitive and it will interpret the string as the variable name and get you the value:
to test
let dia-cost 1000
let item-cost ["dia-cost"]
set item-cost first item-cost
show runresult item-cost ; prints "1000"
end

Related

Yaml-Cpp find out if value was quoted

I'm reading in a yaml file with a big map which looks like this:
test_value: '123'
test_value2: 123
test_value3: 1.0
test_value4: true
test_value5: 'some information'
I can parse it to get the values but I want to know the type of the value. I have specified that a double is always written as a double and boolean are always true or false and also that strings have to be always quoted.
Now the problem is that if I want to read it out again with yaml-cpp I don't get the '123' but rather 123 which I then interpret as a int rather then the string it should be.
Is there any possibility that I overlooked to figure out if there where some quotes around it?
I found a way to figure out if it quoted. In a node there is a tag which you can get with node.Tag() this will in my usecase return me a ! or a ? since the ! is only returned on a former string it can be destingueshed like this.

Data validation with RegEx on time-formatted cell

I need to validate time entered in a cell. But the cell HAS to be in date format because I will need it later to extract hours and minutes.
My RegEx will check perfectly if the cell format is plain text, but if I set it to "time" or "date" or "hh:MM", I get an error in the Spreadsheet : "Parameter 1 expects text, but 0.52154 is number type and can't be forced to text type" (approximate translation from French, sorry).
My formula :
=REGEXMATCH(F5,"([0-1][0-9]:[0-5][0-9]:[0-5][0-9])?([0-9]:[0-5][0-9]:[0-5][0-9])?([0-1][0-9]:[0-5][0-9])?([0-9]:[0-5][0-9])?")
Is there a workaround ?
You could convert number to text with formula:
=REGEXMATCH(TEXT(F5,"hh:mm:ss"),regex)
hh:mm:ss converts the number into proper sting that mimics time format.
to make sure, that entered time is in number format, you should also use ISNUMBER function:
=and(ISNUMBER(F5),REGEXMATCH(TEXT(F5,"hh:mm:ss"),regex)
will return false if text was entered.
The value reads:
Function REGEXMATCH parameter 1 expects text values. But '123' is a number and cannot be coerced to a text.
You get an error because a regex cannot be applied to values other than text. Cast the value to text and use the REGEXMATCH:
=REGEXMATCH(TEXT(F5,""),"([0-1][0-9]:[0-5][0-9]:[0-5][0-9])?([0-9]:[0-5][0-9]:[0-5][0-9])?([0-1][0-9]:[0-5][0-9])?([0-9]:[0-5][0-9])?")
^^^^^^^^^^^
Searching and testing again, I came across a variation of the =TEXT(A1,"pattern") formula : = TO_TEXT()
My final formula :
=REGEXMATCH(TO_TEXT(E20);"([0-1][0-9]:[0-5][0-9]:[0-5][0-9])|([0-9]:[0-5][0-9]:[0-5][0-9])|([0-1][0-9]:[0-5][0-9])|([0-9]:[0-5][0-9])")
I must dig a little bit to understand the difference between TO_TEXT() and TEXT()...
The issue was definitely the way you have to format the pattern parameter of the function. If something like 12h45 was entered, the =TEXT(E20;"hh:mm:ss") pattern would return 00:00:00, and the regex would be evaluated as TRUE.

OpenRefine custom text faceting

I have a column of names like:
Quaglia, Pietro Paolo
Bernard, of Clairvaux, Saint, or
.E., Calvin F.
Swingle, M Abate, Agostino, Assereto
Abati, Antonio
10-NA)\u, Ferraro, Giuseppe, ed, Biblioteca comunale ariostea. Mss. (Esteri
I want to make a Custom text facet with openrefine that mark as "true" the names with one comma and "false" all the others, so that I can work with those last (".E., Calvin F." is not a problem, I'll work with that later).
I'm trying using "Custom text facet" and this expression:
if(value.match(/([^,]+),([^,]+)/), "true", "false")
But the result is all false. What's the wrong part?
The expression you are using:
if(value.match(/([^,]+),([^,]+)/), "true", "false")
will always evaluate to false because the output of the 'match' function is either an array, or null. When evaluated by 'if' neither an array nor 'null' evaluate to true.
You can wrap the match function in a 'isNonBlank' or similar to get a boolean true/false, which would then cause the 'if' function to work as you want. However, once you have a boolean true/false result the 'if' becomes redundant as its only function is to turn the boolean true/false into string "true" or "false" - which won't make any difference to the values function of the custom text facet.
So:
isNonBlank(value.match(/([^,]+),([^,]+)/))
should give you the desired result using match
Instead of using 'match' you could use 'split' to split the string into an array using the comma as a split character. If you measure the length of the resulting array, it will give you the number of commas in the string (i.e. number of commas = length-1).
So your custom text facet expression becomes:
value.split(",").length()==2
This will give you true/false
If you want to break down the data based on the number of commas that appear, you could leave off the '==2' to get a facet which just gives you the length of the resulting array.
I would go with lookahead assertion to check if only 1 "," can find from the beginning until the end of line.
^(?=[^\,]+,[^\,]+$).*
https://regex101.com/r/iG4hX6/2

Why do I need to enclose input in quote marks?

I am working on Python2.7.6 and came across the following problem:
x=eval(input("Enter a number between 0 and 1: "))
Here, the input is supposed to create a string, but it's not running unless I wrap input in single quote marks too, check out the following:
x=eval('input("Enter a number between 0 and 1: ")')
Can someone please clarify why the first code wasn't running and the second one worked? It's really frustrating...I'd appreciate your help!
In Python 2, input is just the composition of eval and raw_input. In effect, your first line is:
x=eval(eval(raw_input("Enter a number between 0 and 1: ")))
Typing in 123 will result in the first call to eval passing the integer 123 into the second eval function, which throws a nice error:
TypeError: eval() arg 1 must be a string or code object
Typing in '123' will make the string pass through unmodified, since eval("'123'") == '123'. Since you don't want to evaluate anything, just use raw_input:
x = raw_input("Enter a number between 0 and 1: ")

Need help on getting characters from a string?

I'm currently in an Intro C++ class, and I'm learning about Strings and Member Functions of them.
I have questions that are like this:
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is a string containing the first character of the value of name . So if the value of name were "Smith" the expression's value would be "S".
or
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is a string containing the last character of the value of name . So if the value of name were "Smith" the expression's value would be "h".
or
Assume that word is a variable of type string that has been assigned a value. Write an expression whose value is a string consisting of the last three characters of the value of word . So if the value of word were "biggest" the expression's value would be "est".
I know things like name[0] and name[name.length() - 1], but I don't know how to turn those into a string in one expression. I've been looking for a table or list of member functions that can help me do this, but I'm stuck. Any directions or aid would be great. :D
Take a look at the substr method.
http://www.cplusplus.com/reference/string/string/substr/
I would use substring:
//assume that the string in question is held in a variable with the name str
string s1 = str.substr(0,1);
string s2 = str.substr(str.length() - 2, 1);
string s3 = str.substr(str.length() = 4, 3);
Hopefully that helps!
There are quite a few number of ways to do it. For instance, an std::string is also a standard container of char. So you could begin with an empty string, and append one char to it.