0

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]
Donald_W
  • 1,773
  • 21
  • 35
user3914448
  • 469
  • 1
  • 11
  • 22

2 Answers2

1

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

Community
  • 1
  • 1
Rahul
  • 3,914
  • 1
  • 14
  • 25
1

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

Community
  • 1
  • 1
Manish Patel
  • 4,411
  • 4
  • 25
  • 48