首页 > 解决方案 > NetLogo 字符串连接?

问题描述

我正在尝试将一些字符连接成一个字符串,然后对结果执行一些条件逻辑,但我得到的不是预期的“ABC”,而是“[A][B][C]”。我怎样才能避免这种情况?

编码:

  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"]

谢谢,帕勒

标签: stringnetlogo

解决方案


首先,first是保留的 NetLogo 原语,但我假设您已将代码翻译成英文以便在此处发布(谢谢!)无论如何,我将假设您的变量命名为x1,x2x3而不是first,secondthird.

您的问题源于这样一个事实,即nukleotider-herenukleotider-on记者为您提供了一个代理集而不是单个代理。因此,of将为您提供一个列表而不是单个值。

有很多方法可以解决这个问题,但您应该首先问自己是否确定您正在查看的每个补丁上只会有一个 nukleotider。如果您确定这一点,您可以选择以下方法之一:

从返回的列表中提取第一项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 

从(可能的)单代理代理集中提取代理:

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 

稍微延迟列表提取:

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)

只需变成一个字符串列表并与之进行比较:

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"]

甚至更花哨:

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)
)

好吧,最后一个可能有点矫枉过正。而且会有更多的方法来做到这一点。但希望你能找到一个适合你的...... :-)


推荐阅读