Home > Enterprise >  Netlogo: double quotes with word command
Netlogo: double quotes with word command

Time:09-16

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

CodePudding user response:

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