# in-place update/delete does not work within a function

**URL:** <https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 5, 2010, 5:52pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479 "2010-02-05T17:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramdas](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@Ramdas](https://forum.kx.com/u/Ramdas)\
**Post date:** [February 5, 2010, 5:52pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/1 "2010-02-05T17:52:00Z")

</div>

Hello,this works out side of function --q)table: ( a: `a`b`c; n: (1 2 3))q)update b:`val from `table`tableq)tablea n b-------a 1 valb 2 valc 3 valWhy does this not work..(is this something to do with variable scope? If so is there anydocumentation which explains variable scope?)In version 2.5 - it does not return `type error but does not do updatein placeq)myf:{table: ([] a: `a`b`c; n: (1 2 3)); update b:`val from`table ; :table}q)myfa n—a 1b 2c 3In version 2.6q)myf:{table: ( a: `a`b`c; n: (1 2 3)); update b:`val from`table ; :table}q)myfq)'2010.02.05T11:41:35.519 typeThanks,RT

---

<div class="post-metadata">

**Author:** ![f11](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@f11](https://forum.kx.com/u/f11)\
**Post date:** [February 5, 2010, 7:06pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/2 "2010-02-05T19:06:00Z")

</div>

Hello,In your example table is a local variable.If you change to a global this will work.q)myf:{table::( a: `a`b`c; n: (1 2 3)); update b:`val from`table;:table}q)tableq)myf[]a n b-------a 1 valb 2 valc 3 valq)tablea n b-------a 1 valb 2 valc 3 valRegards,Fintan.On Feb 5, 12:52?pm, Ramdas <rtha...> wrote:&gt; Hello,&gt;&gt; this works out side of function --&gt;&gt; q)table: ([] a: `a`b`c; n: (1 2 3))\> q)update b:`val from `table\> `table&gt; q)table&gt; a n b&gt; -------&gt; a 1 val&gt; b 2 val&gt; c 3 val&gt;&gt; Why does this not work..&gt; (is this something to do with variable scope? ?If so is there any&gt; documentation which explains variable scope?)&gt;&gt; In version 2.5 - it does not return `type error but does not do update\> in place\>\> q)myf:{table: ( a: `a`b`c; n: (1 2 3)); update b:`val from\> `table ; ? ?:table}&gt; q)myf[]&gt; a n&gt; ---&gt; a 1&gt; b 2&gt; c 3&gt;&gt; In version 2.6&gt;&gt; q)myf:{table: ([] a: `a`b`c; n: (1 2 3)); ? update b:`val from&gt; `table ; ? ?:table}\> q)myf\> q)'2010.02.05T11:41:35.519 type\>\> Thanks,\> RT\</rtha…\>

---

<div class="post-metadata">

**Author:** ![Ramdas](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@Ramdas](https://forum.kx.com/u/Ramdas)\
**Post date:** [February 5, 2010, 7:23pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/3 "2010-02-05T19:23:00Z")

</div>

Fintan,I understand that it works on global variables but what do I do if Ido not want to create a global variable?What happens when there are multiple users who connect to remote qprocess and create same global variable name?Thanks,RamdasOn Feb 5, 1:06?pm, fintanq \<fint…\> wrote:\> Hello,\>\> In your example table is a local variable.\>\> If you change to a global this will work.\>\> q)myf:{table::( a: `a`b`c; n: (1 2 3)); update b:`val from\> `table;:table}&gt; q)table&gt; q)myf[]&gt; a n b&gt; -------&gt; a 1 val&gt; b 2 val&gt; c 3 val&gt; q)table&gt; a n b&gt; -------&gt; a 1 val&gt; b 2 val&gt; c 3 val&gt;&gt; Regards,&gt;&gt; Fintan.&gt;&gt; On Feb 5, 12:52?pm, Ramdas <rtha...> wrote:&gt;&gt;&gt;&gt; &gt; Hello,&gt;&gt; &gt; this works out side of function --&gt;&gt; &gt; q)table: ([] a: `a`b`c; n: (1 2 3))\> \> q)update b:`val from `table\> \> `table&gt; &gt; q)table&gt; &gt; a n b&gt; &gt; -------&gt; &gt; a 1 val&gt; &gt; b 2 val&gt; &gt; c 3 val&gt;&gt; &gt; Why does this not work..&gt; &gt; (is this something to do with variable scope? ?If so is there any&gt; &gt; documentation which explains variable scope?)&gt;&gt; &gt; In version 2.5 - it does not return `type error but does not do update\> \> in place\>\> \> q)myf:{table: ( a: `a`b`c; n: (1 2 3)); update b:`val from\> \> `table ; ? ?:table}&gt; &gt; q)myf[]&gt; &gt; a n&gt; &gt; ---&gt; &gt; a 1&gt; &gt; b 2&gt; &gt; c 3&gt;&gt; &gt; In version 2.6&gt;&gt; &gt; q)myf:{table: ([] a: `a`b`c; n: (1 2 3)); ? update b:`val from&gt; &gt; `table ; ? ?:table}\> \> q)myf\> \> q)'2010.02.05T11:41:35.519 type\>\> \> Thanks,\> \> RT- Hide quoted text -\>\> - Show quoted text -\</rtha…\>\</fint…\>

---

<div class="post-metadata">

**Author:** ![nathan\_perrem1](https://avatars.discourse-cdn.com/v4/letter/n/839c29/32.png) [@nathan\_perrem1](https://forum.kx.com/u/nathan_perrem1)\
**Post date:** [February 5, 2010, 7:31pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/4 "2010-02-05T19:31:00Z")

</div>

charset=us-ascii;  
format=flowed;  
delsp=yes

Mime-Version: 1.0 (iPhone Mail 7D11)  
Date: Fri, 5 Feb 2010 14:31:56 -0500  
Cc: Kdb+ Personal Developers   
  
Ramdas,  
You could define the table in a different namespace. Eg: .ns1  
  
Regards,  
Nathan

---

<div class="post-metadata">

**Author:** ![awall1](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@awall1](https://forum.kx.com/u/awall1)\
**Post date:** [February 7, 2010, 1:20am UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/5 "2010-02-07T01:20:00Z")

</div>

Hi Ramdas,

Is there any particular reason that you want to use update in place??

The typical reason that update in place is nice is to save on memory reallocation. Otherwise, you can always assign the result of the update back to a new variable. If the “new” variable has the same name as the old, then memory allocation is not a problem.?

==Outside function scope==

q)table: ( a: `a`b`c; n: (1 2 3)) q)update b:`val from `table

q)table

a n b

-------

a 1 val

b 2 val

c 3 val

==Gives same results as==

q)table: ([] a: `a`b`c; n: (1 2 3))  
q)table:update b:`val from table

q)table

  

a n b
 
-------
 
a 1 val
 
b 2 val
 
c 3 val
 
  

==Which works inside function scope==

q)f:{ table: ( a: `a`b`c; n: (1 2 3));table:update b:x from table;:table}

q)f[`val]

a n b

-------

a 1 val

b 2 val

c 3 val

  

\> What happens when there are multiple users who connect to remote q  
process and create same global variable name?  

  

The second user's definition will overwrite the first user's definition, unless you have some sort of setup that each user's queries go into a seperate user namespace...??

  

Regards,

Andrew?

---

<div class="post-metadata">

**Author:** ![Lindqvist](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@Lindqvist](https://forum.kx.com/u/Lindqvist)\
**Post date:** [February 7, 2010, 12:14pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/6 "2010-02-07T12:14:00Z")

</div>

Regarding “per user namespace” problems, this can be one way of doing  
a crude separation of users sharing a Q process:

.z.pg:{  
0N!"User, get: ",string .z.u;  
system “d .usr.”,string .z.u;  
x y} .z.pg

.z.ps:{  
0N!"User, set: ",string .z.u;  
system “d .usr.”,string .z.u;  
x y} .z.ps

.z.pc:{  
0N!“User, close: “,string .z.u;  
delete from `$”.usr.”,string .z.u}

---

<div class="post-metadata">

**Author:** ![denghao8888](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@denghao8888](https://forum.kx.com/u/denghao8888)\
**Post date:** [November 14, 2014, 1:30pm UTC](https://forum.kx.com/t/in-place-update-delete-does-not-work-within-a-function/6479/7 "2014-11-14T13:30:00Z")

</div>

This still don’t have an answer.  
Is this by design?
