kdb+

How to map nested list elements to corresponding ids creating a uid table column?


Two variables:

q) indexList
,1452750
,966036
,1125386
,1450294
,1450293
,1452030
,1570712
421818 421840 421897 421988 422026 422059 422084
851199 851271
695902 695928
,933185
679878 679882 679886 679912
,1200606
,548797
,1057031
,751900
508978 509033 509050 509055 509060 509066 509116 509135 509140 509170 509183
,562289
,553980
1199177 1199178
q) uid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3..

I am trying to add the uid for each index to a new column in my table called uid.

So uid[0] should be assigned to indexList[0] and so on.

I'm not sure when dealing with nested lists what the best approach is as all of 421818 421840 421897 421988 422026 422059 422084 would require the same uid e.g. 8

What are the best approaches for manipulating nested list data - transforming it from its current form to a new table column?


Solution

  • q code to create indexList

    q)indexList:(enlist 1452750;enlist 966036;enlist 1125386;enlist 1450294;enlist 1450293;enlist 1452030;enlist 1570712;421818 421840 421897 421988 422026 422059 422084;851199 851271;695902 695928;enlist 933185;679878 679882 679886 679912;enlist 1200606;enlist 548797;enlist 1057031;enlist 751900;508978 509033 509050 509055 509060 509066 509116 509135 509140 509170 509183;enlist 562289;enlist 553980;1199177 1199178)
    

    Note uid trimmed to length of indexList as question would suggest they should match in length.

    q)uid:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    

    Create a sample table and create a uid column of nulls:

    q)tab:([] a:til 2000000)
    q)update uid:0N from `tab
    

    Insert the uids at the indexes we need:

    q)tab[`uid]:@[tab`uid;raze indexList;:;raze (count each indexList)#'uid]
    

    View populated records in output:

    q)select from tab where i in raze indexList
    a       uid
    -----------
    421818  8
    421840  8
    421897  8
    421988  8
    422026  8
    422059  8
    422084  8
    508978  17
    509033  17
    509050  17
    509055  17
    509060  17
    509066  17
    509116  17