Search code examples
kdb

How to sum consecutive identical numbers in a list in kdb?


I have a list like this:

a:1 1 1 1 2 3 1 1 4 4 4 5 6 4

How can I sum all of the consecutive identical numbers in a, so that it will become:

a:4 2 3 2 12 5 6 4

Solution

  • There are many ways - one method:

    q) a:1 1 1 1 2 3 1 1 4 4 4 5 6 4
    q) sum each where[differ a] _ a
        4 2 3 2 12 5 6 4