parameterise table name

Hi,

I’m wondering how to create a table with it’s name as the input to a function.

eg I would like something like this:

func:{[t] 

                      t::( date:date$();time:time$();size:float$();amount:float$());

        };

So, if I wish to create my (empty) global table say called table1 I just do this:

func[table1]      

Of course this doesn’t work. Any suggestions of how to best go about this? i.e. have an input to a function which will globally define a table with the input name.

thanks,

John.

Use http://code.kx.com/wiki/Reference/set:<o:p></o:p>

<o:p> </o:p>

q) func:{[t]  t set ( date:date$();time:time$();size:float$();amount:float$()); };<o:p></o:p>

q) func[`table]<o:p></o:p>

q) `table in tables<o:p></o:p>

1b<o:p></o:p>

<o:p> </o:p>

From: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] On Behalf Of John Smith
Sent: Monday, June 22, 2015 1:30 PM
To: personal-kdbplus@googlegroups.com
Subject: [personal kdb+] parameterise table name<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

Hi,<o:p></o:p>

<o:p> </o:p>

I’m wondering how to create a table with it’s name as the input to a function.<o:p></o:p>

<o:p> </o:p>

eg I would like something like this:<o:p></o:p>

<o:p> </o:p>

func:{[t] <o:p></o:p>

                      t::( date:date$();time:time$();size:float$();amount:float$());<o:p></o:p>

        };<o:p></o:p>

<o:p> </o:p>

So, if I wish to create my (empty) global table say called table1 I just do this:<o:p></o:p>

<o:p> </o:p>

func[table1]      <o:p></o:p>

<o:p> </o:p>

Of course this doesn’t work. Any suggestions of how to best go about this? i.e. have an input to a function which will globally define a table with the input name.<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

thanks,<o:p></o:p>

<o:p> </o:p>

John.<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>


Submitted via Google Groups

Hi John, 

The following should work. 

Marcus 

f:{[t]

         t set ( date:();time:();price:())

}

f['table]

P.s. (I had no back tick symbol on my phone so used an apostrophe for the function input in the line above)

—
Sent from Mailbox

On Mon, Jun 22, 2015 at 9:29 PM, John Smith <js610308@gmail.com> wrote:

Hi,

I’m wondering how to create a table with it’s name as the input to a function.

eg I would like something like this:

func:{[t] 

                      t::( date:date$();time:time$();size:float$();amount:float$());

        };

So, if I wish to create my (empty) global table say called table1 I just do this:

func[table1]      

Of course this doesn’t work. Any suggestions of how to best go about this? i.e. have an input to a function which will globally define a table with the input name.

thanks,

John.


Submitted via Google Groups

add a couple of params, and you have yourself a neat wee function…

f:{[t;ty;c] t set flip c!ty$:()}

/t=table name

/ty=column types

/c=column names

q)f[`tab1;“SIJC”;`c`c1`c2`c4]

q)meta tab1

c | t f a

–| -----

c | s

c1| i

c2| j

c4| c

Might be useful to you..

Thanks,

Sean

Thanks Sean & David!! This exactly what I need, I will test this out in the mornin.

thanks,

John.

Thanks Marcus. 

Sean, may I also ask, in your example:

f:{[t;ty;c] t set flip c!ty$:()}

/t=table name

/ty=column types

/c=column names

f[`tab1;“SIJC”;`c`c1`c2`c4]

Do you know what to use to initialise a list of floats, eg to replace this (see prices):

func:{[t]  t set ( date:date$();time:time$();size:float$();amount:float$();prices:()); };

The last field (prices) I’ve left not specified because I don’t know how to initialise with a list of floats. I don’t actually need to, and func works for me because on the first insert into the table I insert a list of floats to prices and the type is set.

Doing a meta on the table then shows a type F meaning list of floats i assume.

In your example, I cannot get around this problem because I have to specify some letter defining the type. I’d have thought (as seen when doing a meta) the sensible approach would be to use capital letters to specify lists of a certain type, while using small letters for the atomic types, but as seen in your example capitals are used for atom type already. Any thoughts?

regards,

John

This should do it.

q)f[tab1;"SIJ*";cc1c2c4] tab1
q)meta tab1

c t f a
c s
c1 i
c2 j
c4
q)meta tab1 upsert enlist cc1c2c4!(`a;1i;2j;2 3 4f)
c t f a
-----
c s
c1 i
c2 j
c4 F

Thanks Rory

Just what i needed! thanks Rory.