gazi
March 19, 2025, 2:18am
1
https://learninghub.kx.com/forums/topic/assigning-upd-inside-a-function-and-replay-tp-log
I have created sample tp logs and I want to replay it. However, if the upd function is defined inside a function, the error occurred when I replay. Is it related to scope or namespace? Thanks.
I've simplified my code and put it below.
Create TP log
tableName:`students;
students:([] id:1 2; name:`Foo`Bar);
:tpLog set (); h:hopen
:tpLog;
{h enlist(`upd;tableName;value x)} each select from tableName;
hclose h
\\
(The code formatting mess up and I can't fix it. Please ignore those html tags.)
//Load TP log (with error)
students:([]id:`long$(); name:`symbol$());
f:{[x]
upd:insert;
-11!`:tpLog
}["foo"];
//Load TP log (without error)
students:([]id:`long$(); name:`symbol$());
upd:insert;
-11!`:tpLog
The error
'upd
[3] /Users/gazi/src/kdb-test/test.q:4:
upd:insert;
-11!`:tpLog
^
}
Yes this is a scoping issue. upd in f is a local variable only. You can use set to assign the global variable from within a function.
students:([]id:`long$(); name:`symbol$());
f:{[x]
`upd set insert;
-11!`:tpLog
}["foo"];
gazi
March 20, 2025, 2:08pm
3
Thanks. I have tried that but the same error occurred.
https://code.kx.com/q/basics/syntax/#prefix-infix-postfix
Due to insert being a keyword in the language which can operate with infix notation .i.e
`table insert (1;`Foo) The `upd set insert creates a composition rather than executing as you want
https://code.kx.com/q/ref/compose/#implicit-composition
q)`upd set insert k){$[@x;.[x;();:;y];-19!((,y),x)]}[`upd]insert q)type[`upd set insert] 105h
To bypass this you can use either square or round brackets as follows:
{[x] set[`upd;insert];-11!`:tpLog} {[x] `upd set (insert);-11!`:tpLog} :: when executed inside a function can also do global assignment but again needs brackets to execute as you wish:
https://code.kx.com/q/basics/syntax/#colon-colon
{[x] upd::(insert);-11!`:tpLog}
gazi
March 21, 2025, 1:53am
5
I see. Then I need to be careful about that in the future. Thanks for the help.