cancel
Showing results for 
Search instead for 
Did you mean: 

Lists - index summation

ll12877
New Contributor
Hello there,

I am attempting to sum every term in a list to its associated index.
so;
list1: 5 2 4 8 5
desired result:
list1: 6 4 7 12 10

My approach would be to write a loop that keeps going until it has completed the 'size' of the list

while counter <= size
     list1[counter] == list1[counter] + counter
     counter++
end

However, I am unsure what the syntax for this in q is. Any help would be greatly appreaciated.

Thanks,

1 REPLY 1

charlie
New Contributor II
New Contributor II
n.b. kdb+ uses index origin of 0

to get your desired output, a couple of ways:

q)list1+sums count[list1]#1b
6 4 7 12 10
q)list1+1+til count list1
6 4 7 12 10