kdb q: '{ error because of newline?

1.8k views Asked by At

I'm a q newbie and am trying to write a csv save function

CSVsave:{[filename;table]
    filename: $[-11h = type filename;filename;`$":", filename];
    @[hdel;filename;()];
    h: hopen filename;
    (neg h) csv 0: table;
    hclose h;
};

When I put this into a file IO.q and do

\l IO.q

I get an error message

k){0N!x y}
'{
@
"q"
"CSVsave:{[filename;table]\n    filename: $[-11h = type filename;filename;`$\..

but if I remove the new line and put everything in one line

CSVsave:{[filename;table]     filename: $[-11h = type filename;filename;`$":", filename];    @[hdel;filename;()];    h: hopen filename;    (neg h) csv 0: table;    hclose h;};

It runs fine.

Am I missing something obvious?

3

There are 3 answers

1
John at TimeStored On BEST ANSWER

A space before the last parenthesis.

CSVsave:{[filename;table]
    filename: $[-11h = type filename;filename;`$":", filename];
    @[hdel;filename;()];
    h: hopen filename;
    (neg h) csv 0: table;
    hclose h; };

I'd also suggest trying a kdb IDE. Rather than having to continuously save load. e.g. qStudio

0
daveonhols On

Any multi line code needs to be indented in a script. Most people indent their function bodies anyway so only notice on the closing brace. If you have a script like

select ...
from ...
where ...

then the lines starting with "from" and "where" need to be indented too. Or at least last time I tried it.

Also, I recommend kdb studio very highly! I can't imagine developing without it.

0
Himanshu Gupta On

I spent a day few years back when I was new to q trying to figure out what I did wrong. The answer is that you need a space before the ending curly brace. It can be on its own line but just needs a space before it. Why? No idea. That's just how it is.

CSVsave:{[filename;table]
    filename: $[-11h = type filename;filename;`$":", filename];
    @[hdel;filename;()];
    h: hopen filename;
    (neg h) csv 0: table;
    hclose h;
 };