user3914448
user3914448

Reputation: 499

How to get range of elements in a list in KDB?

For example, I have this list:

test:(8;12;15;19;10)

How may I select elements 2 to 4?

When I try list[2;4] it doesn't work for me.

Upvotes: 2

Views: 3632

Answers (3)

nyi
nyi

Reputation: 3229

As answered by Manish, til is the best bet here. You can define a simple function range using til to give the index range :

q)test:(8;12;15;19;10)

q)range:{x+til 1+abs[y-x]}   //include the start and end index 
q)test range[2;4] 
15j, 19j, 10j

Upvotes: 0

Manish Patel
Manish Patel

Reputation: 4491

indexing a list is by far the fastest way.

q)a
8 1 9 5 4 6 6 1 8 5 4 9 2 7 0 1 9 2 1 8 8 1 7 2 4 5 4 2 7 8 5 6 4 1 3 3 7 8 2..
q)\t do[100000;2 3 sublist a]
109
q)\t do[100000;a 2 3 4]
15

So just follow your list with a list of indexes. BTW you can create indexes with til

q)til 2
0 1
q)2+til 2
2 3

Upvotes: 2

Jolene D
Jolene D

Reputation: 11

You can use sublist for this

test:(8;12;15;19;10);
2 3 sublist test 

This will return three elements from the list starting at index 2.

Upvotes: 1

Related Questions