Search code examples
kdb

How do I search a list of booleans for the first true?


I'm trying to find the index of the first true value:

1b?001b

Expected result: 2
Actual result: ,0b

Two questions:

  • How do I solve this issue?
  • What is my code actually doing?

Solution

  • For your first question, you want to use the where keyword, which when passed a boolean list will return the indices of the true values:

    q)where 00101b
    2 4
    

    Use first in front of the above to obtain what you want.

    For your second question, I believe q is casting the left argument of to an int/long and then 'rolling' on the right argument list (see https://code.kx.com/q/ref/deal/).

    Example:

    q)0b?111b
    `boolean$() //return 0 random elements from 111b
    q)1b?111b
    ,1b //return 1 random element from 111b