# table with dynamic columns

**URL:** <https://forum.kx.com/t/table-with-dynamic-columns/11486>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 12, 2017, 10:00pm UTC](https://forum.kx.com/t/table-with-dynamic-columns/11486 "2017-02-12T22:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vortexsbkny](https://avatars.discourse-cdn.com/v4/letter/v/45deac/32.png) [@vortexsbkny](https://forum.kx.com/u/vortexsbkny)\
**Post date:** [February 12, 2017, 10:00pm UTC](https://forum.kx.com/t/table-with-dynamic-columns/11486/1 "2017-02-12T22:00:00Z")

</div>

Hi,

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

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

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

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [February 12, 2017, 11:33pm UTC](https://forum.kx.com/t/table-with-dynamic-columns/11486/2 "2017-02-12T23:33:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkeenan1](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mkeenan1](https://forum.kx.com/u/mkeenan1)\
**Post date:** [February 13, 2017, 1:18am UTC](https://forum.kx.com/t/table-with-dynamic-columns/11486/3 "2017-02-13T01:18:00Z")

</div>

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;&nbsp;

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

?

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

()

0b

`c1`c2!((#;0;0f);(#;0;0f))

/giving…

q)0N!{kols:(`$"c",/:string til x)!0#'x#enlist 0f; flip?[(0#`)!();();0b;kols]}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

/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

---

<div class="post-metadata">

**Author:** ![k112](https://avatars.discourse-cdn.com/v4/letter/k/43a26b/32.png) [@k112](https://forum.kx.com/u/k112)\
**Post date:** [February 16, 2017, 8:15pm UTC](https://forum.kx.com/t/table-with-dynamic-columns/11486/4 "2017-02-16T20:15:00Z")

</div>

f:{flip z!x#/:y$:" "}; //x is number of (null) rows; y are column types; z are column names  
t:f[0;“BXHIJEFCSMDZUVTGNP”;`B`X`H`I`J`E`F`C`S`M`D`Z`U`V`T`G`N`P];

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
