table with dynamic columns

Hi,

How do you create an empty table with columns defined pragmatically: c1, c2, c3, 

This function does not work because I can’t figure out how to add a column. It’s not interpreting column name. 

tbl:{ c:(“c” ,/: (string (1+til x))); t:( c1:“f”$());     f1:{[t;cname] t:update cname:0f    from t; :t };       t:f1[t;c2];    :t  }

q)t:(c1:0#0f)

q)t[`c2]:0#0f

q)t

c1 c2


or

q)t:![t;();0b;enlist[`c3]!enlist 0f]

q)t

c1 c2 c3

--------

http://code.kx.com/wiki/JB:QforMortals2/queries_q_sql#Functional_update

If you knew the column names and number, you could do;

q)flip select c1:0#0f,c2:0#0f from (0#`)!()

c1 c2

-----

Since the columns are dynamic, a functional select may be an approach; 

q)parse"select c1:0#0f,c2:0#0f from (0#`)!()"

?

(!;(#;0;,`);())

()

0b

c1c2!((#;0;0f);(#;0;0f))

/giving…

q)0N!{kols:($"c",/:string til x)!0#'x#enlist 0f; flip?[(0#)!();();0b;kols]}10

+c0c1c2c3c4c5c6c7c8c9!(float$();float$();float$();float$();`flo..

c0 c1 c2 c3 c4 c5 c6 c7 c8 c9

/alternative approach

q)0N!flip{@[(0#)!();x;{0#0f}]}$“c”,/:string[til 10]

+`c0`c1`c2`c3`c4`c5`c6`c7`c8`c9!(`float$();`float$();`float$();`float$();`flo..
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9
-----------------------------
q)

Is this what you were looking for?

Thanks,

Michael

f:{flip z!x#/:y$:" "}; //x is number of (null) rows; y are column types; z are column names
t:f[0;“BXHIJEFCSMDZUVTGNP”;BXHIJEFCSMDZUVTGNP];

meta t


c| t f a

-| -----

B| b

X| x

H| h

I| i

J| j

E| e

F| f

C| c

S| s

M| m

D| d

Z| z

U| u

V| v

T| t

G| g

N| n

P| p