kdb+ q中字符串与数字拼接异常及标准实现方式问询
kdb+ q中字符串拼接的问题解析
1. 多元素拼接变成列表的原因
在kdb+ q中,字符串本质是字符列表(类型为10h)。逗号运算符的行为取决于操作数数量:
- 当用逗号连接两个字符串时,q会执行字符串拼接操作,将第二个字符列表追加到第一个之后,返回一个新的字符串(
10h类型):temp:30 text:"Current temperature is ",string temp show text // 输出:"Current temperature is 30" type text // 输出:10h - 当用逗号连接三个及以上元素时,q会将这些元素构造为一个通用列表(类型为
0h)。此时每个字符串作为独立的字符列表被放入大列表中,显示时会展开所有嵌套的字符,呈现为逐行的单个字符:text:"Current temperature is ",string temp," degree celsius" show text // 输出逐行的单个字符 type text // 输出:0h
2. 使用空分隔符的sv是否为标准做法
是的,用"" sv拼接多个字符串是kdb+中的常用标准做法之一。sv(分隔符连接)函数会将列表中的每个字符串用指定分隔符连接,空分隔符就等价于直接拼接:
text:"" sv ("Current temperature is ";string temp;" degree celsius") show text // 输出:"Current temperature is 30 degree celsius" type text // 输出:10h
除此之外,还有两种常用的拼接方式:
- 使用
raze函数:扁平化嵌套的字符列表,直接拼接成单个字符串:text:raze ("Current temperature is ";string temp;" degree celsius") - 使用迭代拼接运算符
,/:对列表中的元素依次执行逗号拼接操作:text:,/("Current temperature is ";string temp;" degree celsius")
内容的提问来源于stack exchange,提问作者Franz Wong
相关产品推荐
相关产品推荐

