> 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