Search code examples
stringappendkdb

Issue setting up a save path with integer variables and strings in kdb+


I am basically trying to save to data/${EPOCH_TIME}:

begin_unix_time: "J"$first system "date +%s"
\t:1 save `data/"string"$"begin_unix_time"

I am expecting it to save to data/1578377178


Solution

  • You do not need to cast first system "date +%s" to a long in this case, since you want to attach one string to another. Instead you can use

    begin_unix_time:first system "date +%s"
    

    to store the string of numbers:

    q)begin_unix_time
    "1578377547"
    q)`$"data/",begin_unix_time
    `data/1578377547
    

    Here you use the comma , to join one string to another, then using cast `$ to convert the string to a symbol.

    The keyword save is saving global data to a file. Given your filepath, it looks like youre trying to save down a global variable named 1578377547, and kdb can not handle variable names being purely numbers.

    You might want to try saving a variable named a1578377547 instead, for example. This would change the above line to

    q)`$"data/a",begin_unix_time
    `data/a1578377547
    

    and your save would work correctly, given that the global variable a1578377547 exists. Because you are sourcing the date down to the second from linux directly in the line you are saving a variable down to, this will likely not work, due to time constantly changing!

    Also note that the timer system command will repeat it the execution n times (as in \t:n), meaning that the same variable will save down mutliple times given the second does not change. The time will also likely change for large n and you wont have anything assigned to the global variable you are trying to save should the second change.