Jean-Paul
Jean-Paul

Reputation: 21150

Kdb+/q: Looping over initialised list

Let's say I create an empty table:

test:([] name:`symbol$(); balance:`int$());

Now let's populate this list with one row:

insert[`test;(`John;1001)];

Now if I want to loop over this table as follows:

n:0;
k:0;
f:{x%100}
do[count test; k+:f[test.balance[n]]; n+:1]

Then it gave me an error because it tried to use (evaluate) the empty initialisation value with the function f.

Is there any particular reason why this doesn't work?

And how can I make sure it does work?

Upvotes: 0

Views: 1986

Answers (1)

terrylynch
terrylynch

Reputation: 13572

What you're doing may work but it's far from best practice. Loops and indices are not the way to go.

What you're looking for is essentially

test:([] name:`symbol$(); balance:`int$());
insert[`test;(`John;1001)];
insert[`test;(`Jane;2002)];

q)select sum f[balance] from test
balance
-------
30.03

Upvotes: 3

Related Questions