Reference/cut

From Kx Wiki
(Difference between revisions)
Jump to: navigation, search
(Fixed errors and added additional usage)
 
Line 1: Line 1:
= cut =
+
{{refheader|cut|cut verb}}
  
Dyadic function which cuts its right argument at the indices specified by its left argument
+
Verb <tt>cut</tt> splits its right argument according to its left.
 +
 
 +
== Syntax ==
  
 
<pre>
 
<pre>
Line 7: Line 9:
 
</pre>
 
</pre>
  
The result R will have the same number of elements as X and will always be a list.
+
<tt>cut</tt> has two slightly different behaviors, depending on its left argument:
R can be interpreted as
+
 
 +
*if <tt>X</tt> is a single integer, <tt>cut</tt> splits its right argument into <tt>X</tt>-sized parts, for example:
  
 
<pre>
 
<pre>
R[i]=Y[X[i]..(X[i+1]-1)]      where X[i]<X[i+1] and 0<=X[i]<=count Y for all i
+
q)4 cut til 10
 +
0 1 2 3
 +
4 5 6 7
 +
8 9
 
</pre>
 
</pre>
  
i.e
+
*if <tt>X</tt> is a non-decreasing list of integers, it cuts <tt>Y</tt> at the indices given in <tt>X</tt>. The result R has the same number of elements as X and will always be a list. For example:
  
 
<pre>
 
<pre>
q)R:0 3 6 cut 10 20 30 40 50 60 70 80 90
+
q)2 4 9 cut til 10           / the first result item starts at index 2
q)R
+
2 3
(10 20 30;40 50 60;70 80 90)
+
4 5 6 7 8
 +
,9
 +
q)
 +
q)2 4 4 9 cut til 10         / cuts are empty for duplicate indices
 +
2 3
 +
`int$()
 +
4 5 6 7 8
 +
,9
 
</pre>
 
</pre>
  
Cut 10 20 30 40 50 60 70 80 90  at indices 0 3 and 6 which returns the elements from 0 to 2 then from 3 to 5 and then from 6 to end of list.
+
== Remark ==
  
Cut also applies to dictionaries, removing the key in the right argument.
+
<tt>cut</tt> is a simple derivative of the primitive <tt>_</tt> (drop/cut). Where the left argument is an atom, it cuts the right argument into equal sized parts, and otherwise it behaves like <tt>_</tt>. For example:
  
 
<pre>
 
<pre>
q)dict:`a`b`c!(1;2;3)
+
q)"abcde" _ 3                / drop element with index 3
q)dict
+
"abce"
`a`b`c!1 2 3
+
q)"abcde" cut 3              / cut works the same
q)dict cut `b
+
"abce"
`a`c!1 3
+
 
</pre>
 
</pre>
  
If the left argument X is a single number, it will result in Y being split into X-element parts until its end.
+
== See also ==
  
<pre>
+
*[[Reference/Underscore|drop/cut]]
q)R:4 cut 10 20 30 40 50 60 70 80 90
+
q)R
+
10 20 30 40
+
50 60 70 80
+
,90
+
</pre>
+
  
 
----
 
----
 
For a complete list of functions, see the [[Reference|kdb+ Function Reference]].
 
For a complete list of functions, see the [[Reference|kdb+ Function Reference]].

Latest revision as of 09:51, 12 October 2009

Contents

cut (cut verb)

Verb cut splits its right argument according to its left.

Syntax

R:X cut Y

cut has two slightly different behaviors, depending on its left argument:

q)4 cut til 10
0 1 2 3
4 5 6 7
8 9
q)2 4 9 cut til 10           / the first result item starts at index 2
2 3
4 5 6 7 8
,9
q)
q)2 4 4 9 cut til 10         / cuts are empty for duplicate indices
2 3
`int$()
4 5 6 7 8
,9

Remark

cut is a simple derivative of the primitive _ (drop/cut). Where the left argument is an atom, it cuts the right argument into equal sized parts, and otherwise it behaves like _. For example:

q)"abcde" _ 3                / drop element with index 3
"abce"
q)"abcde" cut 3              / cut works the same
"abce"

See also


For a complete list of functions, see the kdb+ Function Reference.

Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox