# Assigning upd inside a function and replay TP log

**URL:** <https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [March 19, 2025, 2:18am UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174 "2025-03-19T02:18:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazi](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/gazi/32/128_2.png) [@gazi](https://forum.kx.com/u/gazi)\
**Post date:** [March 19, 2025, 2:18am UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174/1 "2025-03-19T02:18:07Z")

</div>

[https://learninghub.kx.com/forums/topic/assigning-upd-inside-a-function-and-replay-tp-log](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
     ^
  }
```

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [March 20, 2025, 10:15am UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174/2 "2025-03-20T10:15:49Z")

</div>

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"];
```

---

<div class="post-metadata">

**Author:** ![gazi](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/gazi/32/128_2.png) [@gazi](https://forum.kx.com/u/gazi)\
**Post date:** [March 20, 2025, 2:08pm UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174/3 "2025-03-20T14:08:44Z")

</div>

Thanks. I have tried that but the same error occurred.

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [March 20, 2025, 2:35pm UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174/4 "2025-03-20T14:35:11Z")

</div>

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}
```

---

<div class="post-metadata">

**Author:** ![gazi](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/gazi/32/128_2.png) [@gazi](https://forum.kx.com/u/gazi)\
**Post date:** [March 21, 2025, 1:53am UTC](https://forum.kx.com/t/assigning-upd-inside-a-function-and-replay-tp-log/14174/5 "2025-03-21T01:53:22Z")

</div>

I see. Then I need to be careful about that in the future. Thanks for the help.
