Utsav
Utsav

Reputation: 5918

What is the value returned from index i from a list in kdb

I can understand that i signify the index of an item in a list but when I use i notation to get value, I get different result in different cases:

/Sorted list with unique elements
q)a: 0 1 2 3 4 5 6 7 / sorted list with 8 unique elements
q)a[i]
,3 / why are we getting 3 here?
q)a: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 / sorted list of 18 unique elements
q)a[i]
,3
/ sorted list with non-distinct elements
q)a: 0 1 2 2 3 4 6 8 9 
q)a[i]
,2
q)a: 0 1 2 2 3 3 4 6 8 9
q)a[i]
,2

/Unsorted unique elements
q)show a:-10?10
6 1 7 3 4 8 5 0 2 9
q)a[i]
,3

/Unsorted non-distinct elements
q)show a:10?10
9 5 2 3 9 5 9 7 6 6
q)a[i]
,3

/ list with nulls
q)a:1 3 5 2 0N 5 0N 6
q)a[i]
,2
q)a:1 5 0N 5 0N 6
q)a[i]
,5

Hence the question arises, what do a[i] return each time?
Does it return random value from the list or is there any logic/rule through which we get output in above cases?

Upvotes: 0

Views: 313

Answers (1)

Cmccarthy1
Cmccarthy1

Reputation: 388

In your case it appears that you have i = 3 set within your q session prior to your initial call of a[i]?

In each of the cases you cite above you're returning the 4th element of the array given indexing starts at 0.

// ascending list of integers
q)show a:til 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// i not defined in the session
q)a[i]
'i
  [0]  i  
// set i to be 3
q)i:3 
// try again
q)a[i]
3
// set a as an unsorted list of floats
q)show a:10?1f
0.3927524 0.5170911 0.5159796 0.4066642 0.1780839 0.3017723 0.785033 0.534709..
// return the ith element 
q)a[i]
0.4066642

Upvotes: 3

Related Questions