Hi,
I wonder if there is a way to create parametric table (i.e. using the string store in a variable as the table name of a table).
For example in a master table I have something like this.
masterTable
instrutment_id | sym
1001 | `AAA
1002 | `BBB
1003 | `CCC
Now for every stock in the master table, I would like to make a table to hold its order book. So I would like to make a few table, and I would like to have their names as
AAABook
BBBBook
CCCBook..
etc.
Any suggestions on how I can achieve this without interfacing with an external language?
Appreciate any helps.
Gary
q)mas:(id:1001 1002 1003;sym:AAA
BBB`CCC)
q){@[.;
$string,“Book”;:;(a:();b:())]} each exec sym from mas
.
.`.
q)\a
AAABook
BBBBookCCCBook
mas
q)AAABook
a b
Thanks Jonny,
I am new to kdb so wondering what does it mean by `. in the second statement.
Also is it a protected execution or a functional @ apply? I read the q for mortal and couldn’t find any clue…
Or are there other material mentioned this better?
Thanks
Gary
@ is amend and `. refers to the global context so youre effectively amending the global text<o:p></o:p>
<o:p> </o:p>
Another to write it would be<o:p></o:p>
<o:p> </o:p>
q){(`$string,“Book”) set (a:();b:())} each exec sym from mas<o:p></o:p>
AAABook
BBBBook`CCCBook<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
From: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] On Behalf Of Gary Chow
Sent: Friday, May 8, 2015 11:29 AM
To: personal-kdbplus@googlegroups.com
Subject: Re: [personal kdb+] Parametric table name?<o:p></o:p>
<o:p> </o:p>
Thanks Jonny,<o:p></o:p>
<o:p> </o:p>
I am new to kdb so wondering what does it mean by `. in the second statement.<o:p></o:p>
Also is it a protected execution or a functional @ apply? I read the q for mortal and couldn’t find any clue…<o:p></o:p>
<o:p> </o:p>
Or are there other material mentioned this better?<o:p></o:p>
<o:p> </o:p>
Thanks<o:p></o:p>
<o:p> </o:p>
Gary
It is an apply, the backtick means the root namespace.
On Saturday, May 9, 2015 at 10:19:57 AM UTC+8, Enoch Lam wrote:
It is an apply, the backtick means the root namespace.
Thanks Enoch and Jonny!
Hi Enoch,
Thank you very much for your reply
So the bracket () in
(aaa) set value
sort of act as the dereferencing, and value will be assigned to the variable name stored as symbol in the variable name. Am I correct.
So now I have a further question. Is the bracket () in the statement specific to the verb “set”, or does it work in similar way in some other verb too? Or is it itself some sort of adverb or verb?
Thanks so much.
Gary
Gary,
not quite. (…) isn’t doing anything special beyond grouping elements in the expression to make sure that things evaluate correctly. If you have `somesym set …, this works fine, but if you wanted to build the name out of a string (which requires that you cast to symbol before setting) then you need the parenthesis there to make sure it casts to symbol first. Otherwise, you end up trying to set with your left-operand as string and casting the result to a symbol, which results in a type error. For example
q)`$“this” set 1
'type
q)(`$“this”) set 1
`this
Hope this helps