kdb

KDB convert file handle to regular path


I am getting a file path as an input. I am trying to load a .q file that exists in that location. Is there a way to undo hsym?

Handle: `:/path/to/schemafile.q Expected output: /path/to/schemaFile.q

I tried 'value' but that isn't working.

I am ultimately trying to save the contents of schemaFile.q in a variable

schemaTab: \l /path/to/schemaFile.q

Thanks!


Solution

  • To remove the : from a hsym

    `$1_ string Handle
    

    But your description of what you want suggests maybe you want the q code as a string?

    schemaTab:read0 Handle 
    

    https://code.kx.com/q/ref/read0/

    This syntax example you show will not work:

    schemaTab: \l /path/to/schemaFile.q
    

    Commands using \ must be at the start of lines only, and cannot be passed variables. To run them anywhere and pass variables use system:

    Handle: `:/path/to/schemafile.q
    system"l ",1_ string Handle
    

    Checking how many additional tables were added:

    Handle: `:/path/to/schemafile.q
    tabs:count tables[]
    system"l ",1_ string Handle
    -1 "Loaded ",string[count[tables[]]-tabs]," tables";
    

    More advanced again would be to use .z.vs:

    https://code.kx.com/q/ref/dotz/#zvs-value-set

    q).z.vs:{[x;y] if[x in tables[];-1 "Defined table:", string x]}
    q)\l schema.q
    Defined table:table1
    Defined table:table2
    

    More advanced tracking:

    q)existingTables:tables[]
    q)newTables:`$()
    q).z.vs:{[x;y] if[x in tables[];$[x in existingTables;-1 "Redefined table: ", string x;[-1 "Defined table: ", string x;newTables,:x]]]}
    q)\l schema.q
    Defined table: table1
    Defined table: table2
    
    q)newTables
    `table1`table2
    q)count newTables
    2
    
    //Running again but showing behaviour if tables get redefined
    q)existingTables:tables[]
    q)newTables:`$()
    
    q)\l schema.q
    Redefined table: table1
    Redefined table: table2
    q)count newTables
    0
    
    // Use \x to remove the custom handler when no longer needed
    q)\x .z.vs