How do I unassign variables in KDB? - kdb

Let's say I've written:
d: 3
How do I then unassign this, if I want to delete this variable?
I tried delete d and _d but neither of these worked.

To delete a variable from the root namespace you can use:
delete d from `.
More information can be found here:
https://code.kx.com/q/ref/delete/

Related

KDB - Clearing out functions, variables, tables from server without exiting or restarting service

I'm currently setting up a server and successfully passing functions and test data to that server.
Is there an eloquent way to clear out all the functions, variables, tables, etc.? Since we are running KDB in a Docker container and accessing it via IP address, I prefer not to have to restart the q session on the server, but rather assign many/all values and functions to :: or null.
At present I'm assuming I have to reassign each function/variable/table to :: or similar to achieve this. It isn't a big issue but I expect to have numerous functions.
\p 5042
h:hopen `:XXX.XXX.XX.XX:5042
h "sq:{x*x}" //send sample function sq to server
h "sq: ::" //assign function sq to nothing (repeat for all variables/functions/tables etc)
hclose h
//check the IP address list of functions to confirm deletion http://XXX.XXX.XX.XX:5042/?\f
Would you be able to give us a bit more information as to why you want to clear out everything (and in particular, why do you want to set functions to null)?
Otherwise, you can do a few things. You can do a delete statement on a namespace to delete everything in it. To do delete all tables/variables/functions in the global namespace, you can do the following.
q)a: 1
q)b: 1 2 3
q)f: {1 + x}
q)value `.
a| 1
b| 1 2 3
f| {1 + x}
q)delete from `.
`.
q)value `.
q)f
'f
[0] f
^
q)
If you want to null them rather than delete them, you could use the system commands a, f and v to get lists of all the tables (a), functions (f) and varialbes (v) in the global (or other) name space, and then use set to set them all to null.
q)f: {1+x}
q)g: {2*x}
q)(system"f")set'(::)
`f`g
q)f
q)g
q)
Is this roughly what you were looking for?
(One obvious problem with this is that you might end up deleting other peoples variables.)
Few extra points to add to Matt's:
.Q.gc[] may be a good idea to run after doing this. This will return any memory to the OS that was being used by those variables in the root namespace e.g. if you had a large table defined before this.
Use an alternative namespace for functions you want to keep after this clearing e.g. .utils. You could even add a .utils.refresh function which clears the root namespace and runs .Q.gc[]

how to use each to pass sets of variables to a function in q

I have a function that deletes and works using a directory and a table and column as variables:
Delete1[dir,t,c]
Another that retruns a set of directories that works:
Paths[dir]
Now I am trying to combine these two using something like "each" to all the directories that Paths[dir] to Delete1 function and I am trying something like this:
Delete1 each (Paths[dir];t;c)
The syntax does not quite work.
You want to use projection. Supplying only the second and third arguments to the Delete1 function creates a new function with just one argument. You can use each between the projection and Paths
Delete1[;t;c] each Paths[dir]
You could use dot apply to this end, you can read more about dot applies here https://code.kx.com/q/ref/unclassified/#apply. It would look like the following:
Delete1 .' (Paths[dir];t;c)
Note if you're using this delete function to delete a column from a table in every partition you only need to delete it from .d file in the last partition. (like in a previous question of yours soft deleting a column from a table in q )

Will it be possible for PowerShell to passing value across to Lua

I'm new in PowerShell and Lua, just wandering will it be possible for PowerShell to passing value across to Lua ?
Example, I have a PowerShell will read through a csv file records and I would like to pass the id from the csv to Lua. Will it be possible and how?
Lua scripts can use command line arguments in the form of the top level vararg operator .... Here's an example:
test.lua
local a, b, c = ...
print('a', a)
print('b', b)
print('c', c)
On command line:
lua test.lua 1 2 3
Output:
a 1
b 2
c 3
So, in your powershell script, replace test.lua with your script, and the 1 2 3 with the CSV ID.
I've tried looking for a good resource for more info on how ... works, but I can't seem to find any. If anyone knows of one, let me know, or suggest an edit, thanks.

How to delete only tables in kdb?

say:
q) \a .x
`a`b
q) \f .x
`f1`f2
I need to delete only the tables a and b but not the functions. Now in this case, I can simply say delete a,b from `.x but is there a way to functionalize this?
You can use the following.
q)tables[] /get list of tables in current namespace
`table1`table2`table3..
q)parse"delete table1 from `." /get the parse tree - needed to functionalise
q)![`.;();0b;tables[]] /combine to delete only tables
`.
q)tables[]
`symbol$()
using namespaces
q)tables `.b
`table3`table4`table5
q){![x;();0b;tables x]} `.b
`.b
q)tables `.b
`symbol$()
Hope this helps
Connor

Delete/remove/drop variable in q

I've created a variable in the :
q)myvar: 1
How can I delete it?
I've tried:
q)delete myvar from `.z
but the namespace `.z is clearly not the right one, as the variable still does exist:
q)myvar
1
The top namespace is actually called `., so the following will work:
q)delete myvar from `.
`.
q)myvar
`myvar
Also, running key `. will show all variable defined in the top namespace.