【问题标题】:How do I use projection to loop without using global variables?如何在不使用全局变量的情况下使用投影进行循环?
【发布时间】:2021-05-13 15:25:45
【问题描述】:

有一个包装函数,想用这个函数继续删除,但我认为只对记录有效。

以下是函数:

testFunc1:{
tab:([]a:`1`1`3`1`2;b:2 4 6 8 10);
tab2:update rowNumber:i from tab;
filter:select from tab2 where a = `1;
if [ ((count filter) > 0); tab2:raze .rm.tab[;tab2;filter] each til count filter];
tab:delete rowNumber from tab2;
tab
}

.rm.tab:{[x;tab;filter]
row:exec rowNumber[x] from filter;
if[(count tab) > 0; newTab: delete from tab where i = row];
:newTab
 }

这个想法是在testFunc1 中有tab 并返回它,因为.rm.tab 正在逐一删除记录。我认为.rm.tab有一个bug,如果只有一条记录可以正常工作,但是如果过滤器中有4条记录循环,输出将返回四次。

不确定如何在不使用全局变量的情况下修复 .rm.tab

【问题讨论】:

  • 您能分享一下您对testFunc1[] 的预期输出吗?
  • 所以它会从行号中删除所有 a=`1 的记录。并输出其余部分。我认为解决方案是 tab2:distinct raze .rm.tab[;tab2;filter] each til count filter] 但这是首先输出然后是不同的。想知道是否有更好的循环解决方案 - 存储循环的每个结果
  • 循环有必要吗?您可以只拥有.rm.tab[;tab2;filter]til count filter,然后将if[(count tab)>0;newTab:delete from tab where any i=/:row] 更改为.rm.tab
  • 为什么不像delete from tab where a=`1那么简单?
  • 过滤器只是一个例子,它将与我函数中的其他内容进行比较

标签: kdb


【解决方案1】:

这就是你要找的吗?我已经从 .rm.tab 的调用处删除了 each 并在 .rm.tab 中使用了 /:(每个右侧)。

代码

testFunc1:{
  tab:([]a:`1`1`3`1`2;b:2 4 6 8 10);
  tab2:update rowNumber:i from tab;
  filter:select from tab2 where a=`1;
  if[(count filter)>0;tab2:.rm.tab[;tab2;filter]til count filter];
  :delete rowNumber from tab2;
 }

.rm.tab:{[x;tab;filter]
  row:exec rowNumber[x]from filter;
  if[(count tab)>0;newTab:delete from tab where any i=/:row];
  :newTab;
 }

结果

q)testFunc1[]
a b
----
3 6
2 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多