Netlogo: double quotes with word command - string

this one should be simple, but..
i would like to define a string in Netlogo which I can then send to the 'run' command. The string needs to include double quotes.
the code below gives "visit Paris"
but I want "visit "Paris" "
'''
to setup
clear-all
let str "Paris"
let to-do (word "visit " str)
show to-do
; gives "visit Paris"
end
'''
Netlogo Help claims that I should escape the double quote with a backslash, but this does not seem to work, at least in Netlogo 6.2.
any help? thanks in advance

For me it works perfectly fine if I use a backslash. The difference here is how Netlogo outputs your result. If you use show, it outputs the entire string with quotes around it. In that case the \ is retained since it signals that the following " is part of the string and not the end of the string. If you however use print, the result is shown without quotes around it and the \ is dropped, as it is no longer needed to signal that the following " is a part of the output.
let str "\"Paris\""
let to-do (word "visit " str)
show to-do ; observer: "visit \"Paris\""
print to-do ; visit "Paris"
As used with my answer to your previous quesiton, it would give you the following, which works:
to go-5
run "change-attribute \"attr3\" patches"
end
See also the output section in the Netlogo programming guide

Related

Netlogo: issue with patches attributes as input variable to procedure

thanks to a fellow stack-overflower, i have recently learned how to use 'run-result' to pass patch attributes as input variables to a procedure. However, i am struggling using the same approach when i want to modify the patch attribute. To clarify, in the code below, i am successfully passing the attribute 'attr1' to Export.List.to.file, but when I pass it to Import.List.from.file, i get an 'this isn't something you can use set on' error in the line 'ask datum [set (run-result #attr) file-read]]'
can anyone help?
to setup
clear-all
let patch-list sort patches
ask patches [set attr1 random 10]
Export.List.to.file "attr1" patch-list "attr1.txt"
Import.List.from.file "attr2" patch-list "attr1.txt"
end
to Export.List.to.file [#attr #patch-list #filename]
let list1 map [ p -> [run-result #attr] of p ] #patch-list
carefully [file-delete #filename] []
file-open #filename
foreach list1 [?datum -> file-print ?datum]
file-close
end
to Import.List.from.file [#attr #patch-list #filename]
file-open #filename
foreach #patch-list [datum -> ask datum [set (run-result #attr) file-read]]
file-close
end
To be perfectly honest, I can't explain exactly why it doesn't work in the second case and does work in the first case. My best guess is that in the first case, the attribute is treated as a reporter, something to be used by other processes. In the second case, the attribute is treated as a property, for which different rules apply.
The workaround I would use is to turn the entire set command into a single string, and then turning it back into a command by using run. To combine the different parts of the command into a single string, you can use word: let commandstring (word "set " #attr-name " random 5"). Notice how I use quotation marks for "set " and " random 5" but I don't use them for #attr-name, since I don't want #attr-name in the final string, but rather the string contained within #attr-name. In my case that would be "attr3".
to go-3
change-attribute "attr3" patches
end
to change-attribute [#attr-name patchset]
let commandstring (word "set " #attr-name " random 5")
show commandstring
; this gives "set attr3 random 5"
ask patchset [run commandstring ]
end

How to replace specific characters in a string in Clojure?

I'm trying to replace all single quotation marks with double quotation marks in Clojure. i.e. if the string id " 'name' " I want it to become " "name" ". How can I do that?
I've been trying to use the replace function like this:
(clojure.string/replace " 'The color is red' " #"'" """)
but it is not working.
You've forgotten to escape the double quotation mark
(clojure.string/replace " 'The color is red' " #"'" "\"")
I do this so often I made some convenience functions:
quotes->double
quotes->single
You can find the details here. You may also find this template project useful, esp. the documentation list at the end.

Reading from a string using sscanf in Matlab

I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.

Is there a way to add quotes to a multi paragraph string

I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";

how to split a string or make chars in vb 2010

I searched but nothing explains how to do this,
for example
Dim sentence as String = "cat is an animal"
if i make a msgbox :
MsgBox(sentence)
it shows
cat is an animal
how to make a msgbox that says
cat
is
an
animal.
Easy way Replace space with new line
as in string words = MyString.Replace(" ","\r\n")
Split would be split on space in to an array , and then join that back up with new lines which is pointless unless you need the array for something else.

Resources