Riley Hun
Riley Hun

Reputation: 2785

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

I want to run the function

{`Security$x}

over a list

order`KDB_SEC_ID

and return the list of values that failed. I have the below, which works, but I'm wondering if there is a neater way to write this without the use of a do loop.

Example Code:

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;

Upvotes: 1

Views: 946

Answers (2)

Mark Kelly
Mark Kelly

Reputation: 1780

I agree that Terry's answer is the simplest method but here is a simpler way to do the method you were trying to help you see how achieve it without using do loops

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

It outputs a 0b if it passes and 1b if it fails resulting in a boolean list. Using where on a boolean list returns the indices where the 1b's occur which you can use to index into order to return the failing rows.

Upvotes: 6

terrylynch
terrylynch

Reputation: 13657

If your test is to check which of the KDB_SEC_ID's can be enumerated against the Security list, couldn't you do

q)select from order where not KDB_SEC_ID in Security

Or am I missing something?

To answer your question in a more general case, you could achieve a try-catch over a list to return the list of fails using something like

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

Upvotes: 4

Related Questions