英文:
The method of dotz .z.u user reassignment
问题
只是想知道为什么第三种方法有效,而前两种方法无效?:
.z.u:`admin // 无声地不起作用(.z.u保持不变)
`.[`.z.u]:`admin // 赋值
@[`.;`.z.u;:;`admin] // 好的!
看起来像是一种文本覆盖,在此之后
delete .z.u from `.
.z.u
恢复正常。也许q检测到了前两种情况中的这些覆盖,并礼貌地要求我们不要这样做..
最初的问题是:为什么这个东西:
h{.z.w{.z.u},`},`
会返回空?让无辜的进程对其他人看起来很糟糕,因为它的.z.u
在.z.w
上调用时变为空。但这是同一个进程 - 没有改变.. 我需要在每个进程上加上这一花哨的线来解决这个问题吗?:
@[`.;`.z.u;:;.z.u]
还是更符合意识形态使用:
0[`.z.u]
来获取进程用户?
奇怪的是对于.z.h
来说,它有不同的行为(如预期)。为什么不删除.z.h
呢 :)?
英文:
Just wondering why does the 3rd method works, if the first ones don't?:
.z.u:`admin // silently doesn't work (.z.u unchanged)
`.[`.z.u]:`admin // 'assign
@[`.;`.z.u;:;`admin] // ok!
It look like a some kind of textual-override, after
delete .z.u from `.
.z.u
gets back to normal. So may be q detects these overrides in the first two cases and politely asks us not doing so..
The original problem was: why the heck this:
h{.z.w{.z.u},`},`
returns empty? and makes innocent process looking bad to others, as it's .z.u
became empty being called on .z.w
. But it is the same process - nothing changed.. do I need to put this fancy line on every process to overcome the issue?:
@[`.;`.z.u;:;.z.u]
or is it more ideologically to use:
0[`.z.u]
to get the process user?
Weird that for .z.h
it is the different behavior (as expected). Why don't remove .z.h
also :)?
答案1
得分: 7
这里并不严格正确,你没有覆盖 .z.u。
通过 @[`.;`.z.u;:;`admin]
你的意思是 "在根命名空间中创建一个名为 .z.u 的新变量"。q 程序确实如此做了:它创建了一个名为 .z.u
的新变量。这个变量的存在方式与变量 a:1
或 var:123
一样。
从 q 的角度来看,这个变量并不存在于 .z 命名空间中,它存在于根命名空间中。
我们可以看到原始的 .z.u
仍然没有被修改,例如,从另一个命名空间检查它的值:
q)@[`.;`.z.u;:;`admin]
`.
q).z.u
`admin
q)\d .test ^
q.test).z.u
`HehirS
虽然我不建议这样做,但你也可以使用其他通常无法分配的名称进行此操作,例如:
q)@[`.;`123;:;"hi"]
`.
q)123
123
q)value`123
"hi"
你还可以尝试不同的 .z 变量,但效果会有所不同:
// 服务器进程
q)\p 9876
q)@[`.;`.z.pg;:;{break}]
`.
q).z.pg
{break}
// 客户端进程
q)h:hopen`::9876
q)h"2+2"
4
因此,在这种情况下,尽管看起来 .z.pg 已被重新分配为 'break',但我们可以看到 .z.pg 的默认行为仍然保留。
还有一点需要注意:
`.[`.z.u]:`admin
这对于任何变量都不起作用。你试图将值分配给 `.[`.z.u]
的结果,这对解释器来说是没有意义的。
英文:
So it's not strictly true that you're overwriting .z.u in this case.
What you're saying with @[`.;`.z.u;:;`admin]
is "within the root namespace create a new variable called .z.u". And q goes and does exactly this: it creates a new variable called .z.u
. This variable exists in just the same way as a variable like, say, a:1
or var:123
.
From q's perspective this variable doesn't exist in the .z namespace, it exists in the root namespace.
We can see that the original .z.u
remains unaltered by, say, checking it's value from another namespace:
q)@[`.;`.z.u;:;`admin]
`.
q).z.u
`admin
q)\d .test ^
q.test).z.u
`HehirS
Although I would not advise this you can also do this trick with other names that would usually be unassignable e.g.:
q)@[`.;`123;:;"hi"]
`.
q)123
123
q)value`123
"hi"
You can also try different .z variables but your millage will vary:
// server process
q)\p 9876
q)@[`.;`.z.pg;:;{break}]
`.
q).z.pg
{break}
// client process
q)h:hopen`::9876
q)h"2+2"
4
So in this case while it looks like .z.pg has been reassigned to 'break when a sync message is received we can see the default behaviour of .z.pg is still preserved.
Also as a side note:
`.[`.z.u]:`admin
This will not work for any variable. You're trying to assign a value to the result of `.[`.z.u]
which doesn't make sense to the interpreter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论