简体   繁体   中英

NetLogo string concatenation?

I'm trying to concatenate some characters into a string, and then do some conditional logic with the result, but instead of the expected "ABC", I get "[A][B][C]". How can I avoid that?

The code:

  let first [basetype] of nukleotider-here with [attached]
  let second [basetype] of nukleotider-on patch-at 1 0 
  let third [basetype] of nukleotider-on patch-at 2 0 

  let kombination (word first second third)

  if kombination = "ABC" [set looking-for "M"]

Thanks, Palle

First, first is a reserved NetLogo primitive, but I'm assuming that you've translated your code into English for the purpose of posting it here (thanks!) In any case, I'm going to assume your variables are named x1 , x2 , and x3 instead of first , second and third .

Your problem stems from the fact that the nukleotider-here and nukleotider-on reporters give you an agentset instead of a single agent. Consequently, of will give you a list instead of a single value.

There are many ways out of that, but you should first ask yourself if you are certain that there will only ever be one nukleotider on each of the patch you're looking at. If you're sure of that, you can choose one of the following approaches:

Extract the first items from the lists returned by of :

let x1 first [basetype] of nukleotider-here with [attached]
let x2 first [basetype] of nukleotider-on patch-at 1 0 
let x3 first [basetype] of nukleotider-on patch-at 2 0 

Extract the agent from the (presumably) single agent agentsets:

let x1 [basetype] of one-of nukleotider-here with [attached]
let x2 [basetype] of one-of nukleotider-on patch-at 1 0 
let x3 [basetype] of one-of nukleotider-on patch-at 2 0 

Delay the list extraction a bit:

let x1 [basetype] of nukleotider-here with [attached]
let x2 [basetype] of nukleotider-on patch-at 1 0 
let x3 [basetype] of nukleotider-on patch-at 2 0

let kombination reduce word (sentence x1 x2 x3)

Just turn into a list of strings and compare against that:

let x1 [basetype] of nukleotider-here with [attached]
let x2 [basetype] of nukleotider-on patch-at 1 0 
let x3 [basetype] of nukleotider-on patch-at 2 0

let kombination (sentence x1 x2 x3)
if kombination = ["A" "B" "C"] [set looking-for "M"]

Even fancier:

let kombination reduce word map [ xs -> [basetype] of one-of xs ] (list
  (nukleotider-here with [attached])
  (nukleotider-on patch-at 1 0 )
  (nukleotider-on patch-at 2 0)
)

OK, that last one might be overkill. And there would be even more ways to do it. But hopefully you find one that works for you... :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM