# Reference/cut

(Difference between revisions)
 Revision as of 20:45, 7 March 2007 (view source) (Fixed errors and added additional usage)← Older edit Latest revision as of 09:51, 12 October 2009 (view source) 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 cut splits its right argument according to its left. + + == Syntax ==

Line 7:                                                                                                                                                                                                                                               Line 9:

− The result R will have the same number of elements as X and will always be a list. + cut has two slightly different behaviors, depending on its left argument: − R can be interpreted as + + *if X is a single integer, cut splits its right argument into X-sized parts, for example:

−                                                                                                       R[i]=Y[X[i]..(X[i+1]-1)]      where X[i]
− i.e + *if X is a non-decreasing list of integers, it cuts Y at the indices given in X. The result R has the same number of elements as X and will always be a list. For example:

−                                                                                                       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

− 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. + 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)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                                                                                                                                      +

− 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 == −
+                                                           *[[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                                                                                                                                           +
−
+ ---- ---- For a complete list of functions, see the [[Reference|kdb+ Function Reference]]. For a complete list of functions, see the [[Reference|kdb+ Function Reference]].

## 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:

• if X is a single integer, cut splits its right argument into X-sized parts, for example:
```q)4 cut til 10
0 1 2 3
4 5 6 7
8 9
```
• if X is a non-decreasing list of integers, it cuts Y at the indices given in X. The result R has the same number of elements as X and will always be a list. For example:
```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"
```