Gary
Gary

Reputation: 2007

How to ignore error/exception during process of a list?

Learning kdb+q. Without loss of generality, let's say if I have a list of file paths and wants to open each one of them, but I want to make sure Q opens whatever it exists. Say if one of the file paths doesn't exist, then continue to process others.

I know protected evaluation can let me handle error @[open_many_files_func; files; errhandler]. But how to make it not fail in the middle and continue with the rest?

Upvotes: 0

Views: 428

Answers (2)

user12704155
user12704155

Reputation:

If it's just existance of the file that you're checking for (and not that the function fails due to a problem with the file), then you could also use the key function to check the existance of the file.

q)system "ls"
"file1"
"file2"
"file4"
q)b: a where count each a: key each `:file1`:file2`:file3`:file4
`:file1`:file2`:file4

Once you have the list of files, you can just do

open_many_files_func each b

https://code.kx.com/q/ref/key/

Upvotes: 1

Adam Bonham
Adam Bonham

Reputation: 615

You can use 'each' to iterate over the files

@[open_many_files_func; ; errhandler] each files

Upvotes: 2

Related Questions