Quick question about using variable names in tables construction

Greetings, 
I am very new to q (kdb+), I have got the book and started reading it yesterday. As I read I try to do some work on the way. I have faced a problem and cannot seem to find the solution in the book just yet. 

I have a table:

ticket px vol


c      3  3  

a      2  8  

a      9  3  

a      2  1  

a      1  6  

b      8  8  

c      4  6  

b      4  7  

c      7  8  

a      9  1  

I need to find when each ticket shows up for the first time in the table. So I do the following

q)aMinIdx: (select ticket from t)?`a

q)bMinIdx: (select ticket from t)?`b

q)cMinIdx: (select ticket from t)?`c

q)aMinIdx

1

q)bMinIdx

5

q)cMinIdx

0

Now, I would very much like to write down these results (i.e aMinIdx, bMinIdx and cMinIdx) into a table for the future reference. So I construct a new table, but I get an error and I do not understand why. 

firstTimeTksIdx:( ticket: ab`c; Indx: aMinIdx bMinIdx cMinIdx)

': Bad file descriptor

Interestingly, if I simply substitute aMinId and the rest for just integers, then it works find. It looks like it doesn’t allow me to use variable names aMinIdx, bMinIdx, cMinIdx. I have checked their type 

)type aMinIdx

-7h

seems to be a standard 64bit long atom (all three variables. I can do:

aMinIdx + 1

2

So I am a bit baffled why I can’t use those when constructing a table. I tried using function “value” in all possible combinations, but it doesn’t seem to help. 

Please, if someone could point out the problem, I would be very glad and very thankful. 

Regards, V.A. 

welcome to the community V.A!
please try

firstTimeTksIdx:( ticket: ab`c; Indx:(aMinIdx;bMinIdx;cMinIdx))

It worked for int literals, e.g. 1 2 3, because juxtaposed int literals parse as an int vector, where as aMinIdx bMinIdx cMinIdx parses as 

aMinIdx[bMinIdx[cMinIdx]]

and it recognizes that bMinIdx is not a file handle, hence the error.

Wrapping them inside a list as (aMinIdx;bMinIdx;cMinIdx) stops that apply and allows it to collapse to a int vector after parsing since the elements are the same primitive type.

hth,

Charlie

Hi,

In order to get the first index of each ticket you can use the following:

q)select first i by ticket from t

ticket| x

------| -

a | 1

b | 5

c | 0

Regards,

Thomas Smyth

AquaQ Analytics

Hi guys, 
Thank you both for your replies. I understand the issue now - thank you so much!

Thomas - that is an awesome trick! Love it! 

I come from JAVA/C++/Matlab and getting used to have no loops will take a while, although Matlab is quite handy without loops too. But my God, q  is a great tool. Can’t wait to learn it all ASAP. I am guessing I will be posting here quite often!