代码之家  ›  专栏  ›  技术社区  ›  Franz Wong

使用kdb+q中的数字连接字符串

q kdb
  •  0
  • Franz Wong  · 技术社区  · 3 年前

    我可以附加一个带数字的字符串。

    temp:30
    text:"Current temperature is ",string temp
    show text
    type text
    

    后果

    "Current temperature is 30"
    10h
    

    但是,如果我再追加一个字符串,它就会变成一个列表。(为什么?)

    text:"Current temperature is ",string temp," degree celsius"
    show text
    type text
    

    后果

    C
    u
    r
    r
    e
    n
    t
     
    t
    e
    m
    p
    e
    r
    a
    t
    u
    r
    e
     
    i
    s
    ..
    0h
    

    我可以使用 sv 用空字符串作为分隔符,这是正常的方法吗?

    text:"" sv ("Current temperature is ";string temp;" degree celsius")
    
    2 回复  |  直到 3 年前
        1
  •  3
  •   Maurice Lim    3 年前

    您应该将其更改为

    text:"Current temperature is ",string [temp]," degree celsius"
    

    您的字符串函数正在处理:temp,“摄氏度”,但您只需要string〔temp〕来连接它。

        2
  •  1
  •   Adrian Mole Chris    3 年前

    添加到 Maurice Lim's answer ,这是因为q计算表达式的方式。表达式是从左到右计算的(这意味着表达式是从右到左计算的)。

    https://code.kx.com/q4m3/4_Operators/#412-left-of-right-evaluation

    因此,串联运算符首先作用于

    temp," degree celsius"
    

    其变为混合列表。混合列表的显示方式与您获得第一个结果的方式完全相同。

    使用方括号,更改求值顺序(因为它将temp绑定到函数字符串),从而进行求值 string[temp] 第一然后连接两个字符串,返回一个简单的列表。

        3
  •  0
  •   user1874594    3 年前

    comma operator 处理 concatenated string 作为一个单独的项目,并创建 list 这些角色中的一个。

    另一方面,当您使用 sv 使用一个空字符串作为分隔符,它将列表中的所有字符串连接到一个字符串中,这是串联所需的行为。

    推荐文章