function not working properly

I am confused with this:  Look at this below. Why are the lines in bold yielding different results?

q)trade

date       open  high  low   close volume   sym


2006.10.03 24.5  24.51 23.79 24.13 19087300 AMD

2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT

2006.10.03 24.1  25.1  23.95 25.03 17869600 AMD

2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT

2006.10.03 24.8  25.24 24.6  25.11 17304500 AMD

2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT

2006.10.03 24.66 24.8  23.96 24.01 17299800 AMD

2006.10.03 27.76 28    27.65 27.87 36452200 MSFT

q)extr

{[t;c;r] select from t where (`$1#'string c) within r}

q)

q)

q)extr[trade;sym;KZ]

date       open  high  low   close volume   sym


2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT

q)

q)

q)select from trade where ($1#'string sym) within K`Z

date       open  high  low   close volume   sym


2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT

2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT

2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT

2006.10.03 27.76 28    27.65 27.87 36452200 MSFT

To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1278)
X-Gm-Message-State: ALoCoQkF203IxDNHhAWUnipTF8dwK/BUtf+dONFWL8XQ9Q0G8D4hHMe+WBzow2O0yHsoZs3z6Ceg

Hi Sam,
you can not use an input of the function as a col name in a query like =
this. What happens when you run “extr[trade;sym;KZ]” is that the =
existing variable named sym will be passed to the function. Try =
“extr[trade;open;15]”, it will not even run, if you do not have “open” =
defined (you will get 'open).
The only way (that I know) to have a query with a dynamic column name is =
to use the functional form of select. This will work as intended:

extr:{?[x;enlist (within;($;enlist `;((';#);1;(string;y)));enlist =
z);0b;()]}

When you write functional forms it is usually a huge help to parse the =
query in q and write the functional form based on the result.
Note that the enlist before “z” is necessary because otherwise q would =
interpret KZ (or whatever limits) as column names.

Best,
Andras

2012.10.07. d=E1tummal, 1:31 id=F5pontban Sam =EDrta:

> I am confused with this: Look at this below. Why are the lines in =
bold yielding different results?
>
> q)trade
> date open high low close volume sym
> ------------------------------------------------
> 2006.10.03 24.5 24.51 23.79 24.13 19087300 AMD
> 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
> 2006.10.03 24.1 25.1 23.95 25.03 17869600 AMD
> 2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
> 2006.10.03 24.8 25.24 24.6 25.11 17304500 AMD
> 2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
> 2006.10.03 24.66 24.8 23.96 24.01 17299800 AMD
> 2006.10.03 27.76 28 27.65 27.87 36452200 MSFT
> q)extr
> {[t;c;r] select from t where ($1#'string c) within r} \> q) \> q) \> q)extr[trade;sym;KZ] \> date open high low close volume sym \> ------------------------------------------------ \> 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT \> q) \> q) \> q)select from trade where ($1#'string sym) within KZ
> date open high low close volume sym
> ------------------------------------------------
> 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
> 2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
> 2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
> 2006.10.03 27.76 28 27.65 27.87 36452200 MSFT
>
>
>
> –
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To view this discussion on the web visit =
https://groups.google.com/d/msg/personal-kdbplus/-/hGNfaxC-Nm0J.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.

Thanks for the information. It makes sense to me. How do I view functional form of query generated by Q?

thanks,

Sam

I found the function ‘parse’. Thanks. This is helpful.

To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1278)
X-Gm-Message-State: ALoCoQmcpVmw/dTM1Uwe3dcg9wwEacxeE0pWAF1nv9aJjf2c6MeVjQ3dxSrWWg/OuYCe9DWg28wF

parse “select from trade where ($1#'string sym) within K`Z”

2012.10.07. d=E1tummal, 9:23 id=F5pontban Sam =EDrta:

> Thanks for the information. It makes sense to me. How do I view =
functional form of query generated by Q?
>
> thanks,
> Sam
>
> On Sunday, October 7, 2012 1:04:35 AM UTC-4, Andr=E1s Boh=E1k wrote:
> Hi Sam,
> you can not use an input of the function as a col name in a query like =
this. What happens when you run “extr[trade;sym;KZ]” is that the =
existing variable named sym will be passed to the function. Try =
“extr[trade;open;15]”, it will not even run, if you do not have “open” =
defined (you will get ‘open).
> The only way (that I know) to have a query with a dynamic column name =
is to use the functional form of select. This will work as intended:
>
> extr:{?[x;enlist (within;($;enlist `;((’;#);1;(string;y)));enlist =
z);0b;()]}
>
> When you write functional forms it is usually a huge help to parse the =
query in q and write the functional form based on the result.
> Note that the enlist before “z” is necessary because otherwise q would =
interpret `K`Z (or whatever limits) as column names.
>
> Best,
> Andras
>
>
>
>
> 2012.10.07. d=E1tummal, 1:31 id=F5pontban Sam =EDrta:
>
> > I am confused with this: Look at this below. Why are the lines in =
bold yielding different results?
> >
> > q)trade
> > date open high low close volume sym
> > ------------------------------------------------
> > 2006.10.03 24.5 24.51 23.79 24.13 19087300 AMD
> > 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
> > 2006.10.03 24.1 25.1 23.95 25.03 17869600 AMD
> > 2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
> > 2006.10.03 24.8 25.24 24.6 25.11 17304500 AMD
> > 2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
> > 2006.10.03 24.66 24.8 23.96 24.01 17299800 AMD
> > 2006.10.03 27.76 28 27.65 27.87 36452200 MSFT
> > q)extr
> > {[t;c;r] select from t where (`$1#'string c) within r}
> > q)
> > q)
> > q)extr[trade;sym;`K`Z]
> > date open high low close volume sym
> > ------------------------------------------------
> > 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
> > q)
> > q)
> > q)select from trade where (`$1#'string sym) within `K`Z
> > date open high low close volume sym
> > ------------------------------------------------
> > 2006.10.03 27.37 27.48 27.21 27.37 39386200 MSFT
> > 2006.10.03 27.39 27.96 27.37 27.94 82191200 MSFT
> > 2006.10.03 27.92 28.11 27.78 27.92 81967200 MSFT
> > 2006.10.03 27.76 28 27.65 27.87 36452200 MSFT
> >
> >
> >
> > –
> > You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> > To view this discussion on the web visit =
https://groups.google.com/d/msg/personal-kdbplus/-/hGNfaxC-Nm0J.
> > To post to this group, send email to personal...@googlegroups.com. =

> > To unsubscribe from this group, send email to =
personal-kdbpl...@googlegroups.com.
> > For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.
>
>
> –
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To view this discussion on the web visit =
https://groups.google.com/d/msg/personal-kdbplus/-/9DxjIUgo5\_MJ.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.