q kdb中更新语句的长度错误

Length error in update statement in q kdb

我有一个 table,我想根据条件更新一行中的几列

q)t:([] id:10 20; l1:("Blue hor";"Antop"); l2:("Malad"; "KC"); pcd:("NCD";"FRB") )

当我使用更新语句时,它抛出'长度错误

q)update l1:"Chin", l2:"Gor" from t where id=10
'length
q)update l1:"Chin", l2:"Gor" from `t where id=10
'length

我在 Q for Mortals 中阅读了下面的内容,但是有没有什么方法可以根据条件更新一行中的几列?

The actions in the Where phrase and the Update phrase are vector operations on entire column lists. This is the Zen of update.

因为你在这里处理字符列表(而不是符号),你需要使用 enlist:

q)update l1:enlist "Chin", l2:enlist "Gor" from t where id=10
id l1      l2    pcd
----------------------
10 "Chin"  "Gor" "NCD"
20 "Antop" "KC"  "FRB"

否则,您正在尝试用长度为 4 ("Chin") 或 3 ("Gor") 的向量更新长度为 1 (t where id=10) 的向量。

请尝试以下语句:

update l1:count[i]#enlist"Chin", l2:count[i]#enlist"Gor" from t where id=10

无论有多少行与 where 子句匹配,它都有效。

更新时,分配列表的长度应等于更新的行数。 Qstring 视为字符列表。这就是为什么当您将 "Chin" 分配给 l1 时,Q 会尝试分配长度为 4 的列表,而预期的列表长度为 1。这会导致 'length 错误。

count[i]#enlist"Chin" 创建 N 个重复值的列表:("Chin";"Chin";...)。其中 N 是更新的行数。这解决了问题

要像这样更新 table,您需要添加 enlist 关键字:

q)update l1:enlist "Chin", l2:enlist "Gor" from t where id=10
id l1      l2    pcd
----------------------
10 "Chin"  "Gor" "NCD"
20 "Antop" "KC"  "FRB"

这是因为您需要添加字符串列表而不仅仅是字符串