Terry
Terry

Reputation: 519

How do I use projection to loop without using global variables?

There is a wrapper function and would like to use the function to keep deleting but I think only works for a record.

Here are the functions:

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
 }

The idea is to have tab in testFunc1 and return it as .rm.tab is deleting the records one by one. I think there is a bug in .rm.tab, if only one record it works fine, but if there are 4 records in filter for looping, the output will return four times.

Not sure how I can fix the .rm.tab without using global variables?

Upvotes: 0

Views: 110

Answers (1)

Cathal O'Neill
Cathal O'Neill

Reputation: 3179

Is this what you're looking for? I've removed the each from where .rm.tab is called & used /: (each-right) inside .rm.tab.

code

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

result

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

Upvotes: 1

Related Questions