KDB:try-catch 列表和 return 失败列表

KDB: try-catch over a list and return list of fails

我想要运行函数

{`Security$x}

在列表中

order`KDB_SEC_ID

和 return 失败的值列表。我有下面的方法,但我想知道是否有更简洁的方法来编写它而不使用 do 循环。

示例代码:

idx:0; 
fails:(); 
do[count (order`KDB_SEC_ID); 
  error:@[{`Security$x};(order`KDB_SEC_ID)[idx];0Nj];
  if[error=0Nj;fails:fails,(order`KDB_SEC_ID)[idx]];
  idx:idx+1;
  ];
  missingData:select from order where KDB_SEC_ID in distinct fails;

如果您的测试是检查 KDB_SEC_ID 中的哪些可以根据 Security 列表枚举,您不能这样做吗

q)select from order where not KDB_SEC_ID in Security

还是我遗漏了什么?

要在更一般的情况下回答您的问题,您可以使用 return 之类的方法对列表进行 try-catch 到失败列表

q){x where @[{upper x;0b};;1b] each x}(2;`ab;"Er";1)
2 1

我同意 Terry 的回答是最简单的方法,但这里有一个更简单的方法来执行您试图帮助您了解如何在不使用 do 循环的情况下实现它的方法

q)SECURITY
`AAPL`GOOG`MSFT

q)order
KDB_SEC_ID val
--------------
AAPL       1
GOOG       2
AAPL       3
MSFT       4
IBM        5

q)order where @[{`SECURITY$x;0b};;1b] each order`KDB_SEC_ID
KDB_SEC_ID val
--------------
IBM        5

如果通过则输出 0b,如果失败则输出 1b,从而生成布尔列表。在布尔列表 return 上使用 where 出现 1b 的索引,您可以使用它索引到 order 到 return 失败的行。