KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ How to remove or add items from a list Re: How to remove or add items from a list

  • Laura

    Administrator
    April 20, 2022 at 12:00 am

    Because underscores can be part of names, separate a name from the Drop operator with spaces.

    q)n:3 q)n_ til 5 'n_ [0] n_ til 5 ^ q)n _ til 5 3 4 q)-2_ 3_ til 10 3 4 5 6 7

    The null keyword will find nulls. You can use where not null to remove all nulls. Or, to remove leading and trailing nulls, you can count and Drop the nulls from each end. Note below the use of the Zen monks idiom to return both the boolean and its reverse.  And commuting (switching the arguments of) Drop in order to use Over to apply it to a list of left arguments.

    q)q:0N 0N 3 4 0N 6 7 0N 8 0N
    q)q where not null q / lose all nulls 3 4 6 7 8 
    q)1 reversenot null q / Zen monks 0011011010b 0101101100b 
    q)?'[;1b]1 reversenot null q 2 1 
    q)q {y _ x}/1 -1*?'[;1b]1 reversenot null q / commuted Drop 3 4 0N 6 7 0N 8

     

    All neat tricks. But if you simply want to drop leading and trailing nulls, just trim them.

    q)trim q 3 4 0N 6 7 0N 8