reading from stdin

405 views
Skip to first unread message

andyturk

unread,
Oct 3, 2009, 1:55:28 AM10/3/09
to Kdb+ Personal Developers
Is there a way to read data from stdin? Some of the tables I'm working
with are essentially code and it's inconvenient to carve them out into
tiny little text files. The usual column-oriented definitions seem a
little odd too when the data to be represented is a set of tuples. I
haven't found anything in the documentation that shows how to read
data from stdin (e.g., a script).

Here's an example to illustrate what I'd like to do:

fsm:("III";enlist ",")0: ``END
s0,symbol,s1
0, 0, 1
0, 1, 0
1, 0, 0
1, 1, 1
`END

Where the symbol `END would be the boundary of the "here table".

Felix Lungu

unread,
Oct 3, 2009, 6:20:26 AM10/3/09
to personal...@googlegroups.com
kdb is not really a general purpose shell. "here document" shell
feature is not implemented and i think it will never be.

Timothy Rieder

unread,
Oct 3, 2009, 9:16:19 AM10/3/09
to personal...@googlegroups.com, personal...@googlegroups.com
You can read from stdin though:

x:read0 0

Tim Rieder

unread,
Oct 3, 2009, 9:23:30 AM10/3/09
to personal...@googlegroups.com
If you really wanted a project, you could probably modify q.k or write your own language to enable something like this. You can also write a C function and load it in to accept keyboard input where enter does not terminate the string.

Aaron Davies

unread,
Oct 3, 2009, 12:17:26 PM10/3/09
to personal...@googlegroups.com
On Oct 3, 2009, at 1:55 PM, andyturk wrote:

> Is there a way to read data from stdin? Some of the tables I'm working
> with are essentially code and it's inconvenient to carve them out into
> tiny little text files. The usual column-oriented definitions seem a
> little odd too when the data to be represented is a set of tuples. I
> haven't found anything in the documentation that shows how to read
> data from stdin (e.g., a script).
>
> Here's an example to illustrate what I'd like to do:
>
> fsm:("III";enlist ",")0: ``END
> s0,symbol,s1
> 0, 0, 1
> 0, 1, 0
> 1, 0, 0
> 1, 1, 1
> `END

the closest you can really come to a table literal in q is to insert a
row at a time and use whitespace to make it kind of look like a table:

% cat fsm.q
fsm:flip`s0`symbol`s1!flip 3#enlist 0#0
`fsm upsert(
0 0 1;
0 1 0;
1 0 0;
1 1 1
);
show fsm
% q fsm.q
KDB+ 2.6 2009.09.15 Copyright (C) 1993-2009 Kx Systems
m32/ 2()core 2048MB

s0 symbol s1
------------
0 0 1
0 1 0
1 0 0
1 1 1
q)

otherwise, yeah, you'll have to hack on the language somehow if you
really want "here-tables" in q

unfortunately the best place to implement that would probably be in
the preprocessing function that handles comments:

k){x:("#!"~2#*x)_x:-1!'x;y{0N!x
y}'"\n"/:'(&~^*:'x)_x@:&(#:'x)&~"/"=*:'x@:&~|':(b?-1)#b:+\-/x~\:/:+,"/\
\";}

but seems to be an internal function hard coded in the q binary, and
so not override-able.

you can see it by feeding a malformed file to q and signaling up the
stack:

% cat foo.q
a:
% q foo.q
KDB+ 2.6 2009.09.15 Copyright (C) 1993-2009 Kx Systems
m32/ 2()core 2048MB

k){0N!x y}
'a
@
"q"
"a:"
q))'
k){x:("#!"~2#*x)_x:-1!'x;y{0N!x
y}'"\n"/:'(&~^*:'x)_x@:&(#:'x)&~"/"=*:'x@:&~|':(b?-1)#b:+\-/x~\:/:+,"/\
\";}
'
q))

you might be able to construct a small language which would transform
here-tables into ordinary hard-coded q tables, then pass the code to q
for further processing, but i wouldn't have any idea how to do that,
beyond advising you to read and understand q.k and s.k

andyturk

unread,
Oct 3, 2009, 8:04:11 PM10/3/09
to Kdb+ Personal Developers
> ... see it by feeding a malformed file to q and signaling up the stack ...

Goat's blood and chicken feathers, I think. ;-)

I like your upsert idea though, which uses q's standard reader to
accomplish
95% of what the "here document" approach would need to implement.
Cool. Thanks!

fsm:([]s0:`int$();symbol:`int$();s1:`int$()) upsert (
/ s0 symbol s1
/-------------
Reply all
Reply to author
Forward
0 new messages