Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading