Reputation: 39
time_snap_temp:((1 2 3 4);(4 5 6 7);(7 8 9 10))
col_names:`AA`AB`AC`AD
f_try: {y!x}[;col_names]
agg:f_try each time_snap_temp
one_dict: f_try time_snap_temp[0]
one_dict is a dictionary as expected but agg is a table. How does q combine dictionaries to create a table in agg?
Thanks and appreciate your help.
Upvotes: 0
Views: 178
Reputation: 1097
A table is a list of like dictionaries; that is, dictionaries with the same key. (Key order matters.)
f_try
could be a simple projection of Dict !
:
q)f_try:`AA`AB`AC`AD!
q)f_try each time_snap_temp
AA AB AC AD
-----------
1 2 3 4
4 5 6 7
7 8 9 10
Or just use Dict Each Right /:
q)`AA`AB`AC`AD!/:time_snap_temp / key is constant
AA AB AC AD
-----------
1 2 3 4
4 5 6 7
7 8 9 10
But if the keys don’t match
q)4 4 4?\:col_names / mix up the keys: Deal Each Left
AC AC AD AC
AA AB AB AC
AD AD AC AB
q)(4 4 4?\:col_names)!'time_snap_temp
`AC`AD`AD`AA!1 2 3 4
`AC`AB`AA`AB!4 5 6 7
`AA`AD`AD`AD!7 8 9 10
If the keys are not identical, the list is just a list of dictionaries. They must be like dictionaries to yield a table.
With a matrix you don’t need to iterate through the rows like this. You can flip it, use Dict to make a column dictionary ( dictionary of same-length columns) and flip back into a table. (The flip
keyword just sets an internal flag, so it is a cheap operation.)
q)flip `AA`AB`AC`AD!flip time_snap_temp
AA AB AC AD
-----------
1 2 3 4
4 5 6 7
7 8 9 10
Upvotes: 0
Reputation: 2800
A table is actually a list of dictionaries, when using f_try
with each it will output a list of dictionaries which get promoted to a table. When just using f_try
with the first entry in time_snap_temp
, just one dictionary is outputted so remains a dictionary.
// see how using 1# and each here will output list of dictionaries of length 1 and also get promoted to a table
q)f_try each 1#time_snap_temp
AA AB AC AD
-----------
1 2 3 4
Upvotes: 2