user3914448
user3914448

Reputation: 499

Get range of elements in KDB using variables

Why I can't use variable inside array ranges in KDB?

test:1 2 3 4 5

This example won't work:

pos:3;
test[1 pos]

but this way it will work

test[1 3]

Upvotes: 0

Views: 538

Answers (2)

Manish Patel
Manish Patel

Reputation: 4491

To understand what you're asking, consider:

1 2 3 7

That is a simple list of integers. Now consider:

a 2 3

Where a is a vector. The above indexes into a. Easy. Now say you want to have that 2 3 list as a variable

b:2 3 a b //works!

You are specifically asking about how to get a range from a list, this is covered in How to get range of elements in a list in KDB?

In that answer, use variables to create your index list and use the result to index into a

Upvotes: 1

Rahul
Rahul

Reputation: 3969

As you can see, when you use test[1 3], (1 3) is a list. So vector variable requires a list.

   q) list1:1 3
   q) test[list1]

So you have to use:

   q)n:3
   q)list1:(1;n)
   q)test[list1]
   q)test[(1;n)]  / alternate way

For detail explanation about why only semicolon doesn't work and why we require brackets '()',check my answer for this post:

kdb/q: how to reshape a list into nRows, where nRows is a variable

Upvotes: 1

Related Questions