list - KDB+ / Q Table creation with dynamic number of columns -


i doing stuff this:

q)seed:5 q)numvals:3 q)id:`u#1+til seed; q)vals:numvals cut (seed*numvals)?`8 q)1!([]id;vals) 

now getting table this:

id| vals                       --| -------------------------- 1 | bhlkdkfd ghmjjepm gphfcddd 2 | jnloahej ipbcbnop heokehhg 3 | eijocfod nbbeopjo ddhiffmp 4 | cndelncn cmcmkckd aelpmccp 5 | nhjdlned cbkgpggg kgbcifbj 

is there way automatically unfold vals list during table generation create table looks this:

id| vals0    vals1    vals2               --| -------------------------- 1 | bhlkdkfd ghmjjepm gphfcddd 2 | jnloahej ipbcbnop heokehhg 3 | eijocfod nbbeopjo ddhiffmp 4 | cndelncn cmcmkckd aelpmccp 5 | nhjdlned cbkgpggg kgbcifbj 

a table dictionary dictionary of symbol keys list of equal length vectors. dynamically create column names using "string til".

q){ `id xkey update id:i flip (`$"vals",/:string til y)!(y#x)?\:`8 }[3;4] id| vals0    vals1    vals2    vals3 --| ----------------------------------- 0 | lkamnmmm nfnbfmkm kiblpojl onhlghno 1 | ehippdmm pocmhfkf edmeodcp iajdgbcc 2 | kcbjofjk ammebbah dajdafek aafdkpao  q){ `id xkey update id:i flip (`$"vals",/:string til y)!(y#x)?\:`8 }[3;6] id| vals0    vals1    vals2    vals3    vals4    vals5 --| ----------------------------------------------------- 0 | hkdnolgf jbfokhef medkopgk objjkcmf hcnelcjh mkmiagdh 1 | kagnelcp lhjkpjen gokacegn iiocipck gpeachbd apmdghdl 2 | bejncmmp mfhnonen klihfepf oieoflli bbgombmk dkbnlhmd 

your key id column isn't necessary, kdb tables have virtual column row number. here use update make visible.

to explain code step step, start right , try evaluating each part.

a. in kdb can call function 2 arguments so: (x 3, y 4)

q){x+y}[3;4] 7 

b. question mark in kdb can used lot of things, when has number on left , `8 on right generates n random symbols of length 8. here use : adverb modify function , call multiple times for-each item on left:

q){(y#x)}[3;4] 3 3 3 3 q){(y#x)?\:`8}[3;4] gknafbmp odolnkpd pblgicmp aphcdfab mgidjeap iledgchk ppinbcgh kcijdnbg jleeoccb ljjdnami affhgjlm cnijgipc 

c.generate column names concatenating strings , again using adverb:

q){(`$"vals",/:string til y)}[3;10] `vals0`vals1`vals2`vals3`vals4`vals5`vals6`vals7`vals8`vals9 

d. table in kdb flipped dictionary. simple example:

q)d:`a`b!(1 2 3;4 5 6) q)d a| 1 2 3 b| 4 5 6 q)flip d b --- 1 4 2 5 3 6 

i use technique generate table:

q){(`$"vals",/:string til y)!(y#x)?\:`8 }[3;5] vals0| djgndbde hlggaadm pbofgnac vals1| goppgmfe jlfpmlab ibgkihem vals2| npklikob okkmmlbi llnbhgha vals3| lbekogce kjclledc cdjbmdpm vals4| okmleidn lebjkagh cenblgbi q){flip (`$"vals",/:string til y)!(y#x)?\:`8 }[3;5] vals0    vals1    vals2    vals3    vals4 -------------------------------------------- iiifjhhi idinahfa iejibpcl hebikhmc cjlegpke jicockjo kpjphpof kfimnmgh fpjolkmb mkkjldfc acjfnnpd ciflaggo ennfgapg bbodkdla bichmhpj 

e. update using virtual column , use xkey make keyed table:

q){ `id xkey update id:i flip (`$"vals",/:string til y)!(y#x)?\:`8 }[3;4] id| vals0    vals1    vals2    vals3 --| ----------------------------------- 0 | mkfcpkdg dnhmifmf gedbdmkb dpdcdhib 1 | efhafoeh jpidfdno fdbddhgn fimolnmb 2 | jjmeickp clkbenoe lndodeel pgbfojdb 

i advise using technique break down kdb code try , understand it. work right left, querying smallest part don't understand...until do.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -