How do I sort?

Short answer: asc and desc:

q)asc 3 5 2 0 1
`s#0 1 2 3 5
q)desc 3 5 2 0 1
5 3 2 1 0
q)

Notice that, in the case of asc, q attaches the `s attribute to the result. (There is no corresponding attribute for a descending sort.)

As an aside, you may be surprised to discover that calling asc on an attribute-free list that happens to be in sorted order will have the `s attribute applied to it as a side effect:

q)list: til 1000
q)attr list
`
q)asc list
`s#0 1 2 3 4 5 6 7 8 9 10 ..
q)list
`s#0 1 2 3 4 5 6 7 8 9 10 ..
q)

Also take a look at iasc and idesc.

Finally, to sort a table instead of a list, see xasc and xdesc. Read more about sorting tables from this faq

What happens if I duplicate a partition name across multiple segments?

The content of the table is split across n segments. For example, par.txt specifies 3 segments below and slices a table named vltablename by symbol into 3 segments.

For example:

$ cat /vol/db/par.txt
/vol/disk1/segment
/vol/disk2/segment
/vol/disk3/segment

$ ls /vol/disk*/segment/2011.03.07/vltable
/vol/disk1/segment/2011.03.07/vltable # sym A-I
/vol/disk2/segment/2011.03.07/vltable # sym J-R
/vol/disk3/segment/2011.03.07/vltable # sym R-Z

Note that the typical `p# attribute of column sym will be missing in the query result. Remember to re-apply the attribute prior to use.

See also: .Q.u, and .Q.par

Is there a free trial version of kdb available for download?

Yes. Note the following license restrictions:

  • Usage is limited to non-commercial use.
  • Process lifetime is limited to 90 minutes per instantiation.
  • The license expires after 90 days after release.
  • 32-bit only.

The binary and complete terms of usage are here.

I’m used to sprinkling print statements to debug my code. How can I do the same in q?

Like printf, 0N! is your friend. When you place 0N! before an expression, it prints the result of that expression to the console and then returns it:

q)1 + 0N! til 10
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
q)

0N! does change the functionality of your code in one subtle, albeit usually harmless, way. If you place 0N! before a modifier-assignment operator (e.g. +:, -:), then the result of that assignment is no longer null; it is the value assigned:

q){x +: 47} 1
q){0N! x +: 47} 1
48
48
q)

See also: .Q.s and .Q.s1

How do I debug code in kdb?

You can enable error trapping via the -e command line option at kdb start-up:

$ q -e 1

or from the q console

q)\e 1

Upon an error, kdb halts and outputs the body of the function (.z.s (self)) in which the error occurred as well as an error message. You are free to inspect the values of any global or local variables to try to diagnose the source of the problem. At this point you have the following options:

  • type '(single quote) to pop the stack.
  • type :(colon) to resume execution
  • type \(slash) to exit debug mode

There is no ability step into a function call or move up and down the stack.

What data structures does q provide out of the box?

The q language has only 3 non-atomic data structures:

  • lists
  • dictionaries
  • tables

However, q does use other structures internally (e.g., hash tables) to enhance the performance of operations on the above language-level structures.

Does q pass-by-value or pass-by-reference?

Semantically, q is pass-by-value. However, q only copies values of structures (i.e., lists, dictionaries, or tables) if they are modified by the callee, so q often performs as well as pass-by-reference.

What do you do if you want to have the callee modify variables passed by the caller? As our reader cyprus points out, you can pass the name of a global variable:

modifier: {[variable_name]
    variable_name set 47;
    }

caller: {[]
    global:: "global";
    modifier `global;
    show global; // prints 47
    }

Whether this should be called pass-by-reference is the subject of many flame wars; Wikipedia's entry on Evaluation strategy provides a cogent, balanced explanation of the varying interpretations of that (and related) terms. What's important to know are the following:

  • it is impossible for a callee to modify a caller's local variables, and
  • passing a large structure between functions is inexpensive until a callee modifies it.

Is it possible to hold partitioned table and unpartitioned table named the same on a single kdb process?

No. Because they share the same namespace, you can only have one type or the other.

Page 1 ... 8 9 10 11 12