access local variables in value

562 views
Skip to first unread message

CL Jason

unread,
May 26, 2012, 11:30:28 AM5/26/12
to personal...@googlegroups.com
Hi,
In function f I need to use to value to evaluate dynamic strings where
some variables are embedded, however, I find it's not possible to
access the function's local variables inside the evaluation, though
global variables are ok. For example,
q)f:{a:100;value "a"}
q)f[]
{a:100;value "a"}
'a
.:
"a"
q))a
100
q))value "a"
100

q))g:{b::200;value "b"}
q))g[]
200

I don't want to have too many global vars. Are there some solutions?
Thanks a lot!

Paweł Tryfon

unread,
May 26, 2012, 11:44:30 AM5/26/12
to personal...@googlegroups.com
If you need to use dynamic strings you can just inline the values into
the strings:
q)f:{a:100;value string a}
q)f[]
100
q)g:{a:100;value "5+",string a}
q)g[]
105

But I would first ask a question if dynamic strings are really needed.
Why can't you just use lambdas?

2012/5/26 CL Jason <cl.j...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
> 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.
>

CL Jason

unread,
May 26, 2012, 12:27:29 PM5/26/12
to personal...@googlegroups.com
value string a is different from value "a"

the reason I need dynamic string is because I need to construct some
queries dynamically where some terms are local variables generated at
run-time, e.g.:
\l sp.q

q)f:{[]slocal:select s,city from s; v:value "select count s by city
from slocal",many other where conditions;...many other processing of v
and slocal...}
q)f[]
{[]slocal:select s,city from s; value "select count s by city from slocal"}
'slocal
.:
"select count s by city from slocal"

How to replace this slocal by string slocal? When using lambda, there
are issues of visibility of global/local vars

Kim Kuen Tang

unread,
May 26, 2012, 12:35:28 PM5/26/12
to personal...@googlegroups.com
Am 26.05.2012 18:27, schrieb CL Jason:
> value string a is different from value "a"
>
> the reason I need dynamic string is because I need to construct some
> queries dynamically where some terms are local variables generated at
> run-time, e.g.:
> \l sp.q

For this kind of work you should use functional select.

https://code.kx.com/trac/wiki/QforMortals2/queries_q_sql#Functionalselect

HTH
Kim

>
> q)f:{[]slocal:select s,city from s; v:value "select count s by city
> from slocal",many other where conditions;...many other processing of v
> and slocal...}
> q)f[]
> {[]slocal:select s,city from s; value "select count s by city from slocal"}
> 'slocal
> ..:

Aaron Davies

unread,
May 26, 2012, 3:26:20 PM5/26/12
to personal...@googlegroups.com
In function f I need to use to value to evaluate dynamic strings where
some variables are embedded, however, I find it's not possible to
access the function's local variables inside the evaluation, though
global variables are ok.

yes, only globals are accessible through get of string code (or eval of parse tree)

globals have real names and are accessible for reflection, locals (and params) don't and aren't


--
Aaron Davies
aaron....@gmail.com

Sean

unread,
May 27, 2012, 9:35:51 AM5/27/12
to Kdb+ Personal Developers
that's one problem bugging me for long, is there any way to list down
all variables in debug mode?

q)f
{a:100;break}
q)f[]
{a:100;break}
'break
q))\v
`symbol$()
q))key `.
,`f
q))
q))
> aaron.dav...@gmail.com

Oleg Zakharov

unread,
May 27, 2012, 10:03:29 AM5/27/12
to personal...@googlegroups.com
Hi, Sean!

While there's a simple way to list names of locals 
(try 
@[;2]value .z.s 
while in break)

There appears no obvious way of listing/dumping their values, other than type/feed them to debug console one by one.

Would be grateful if somebody proves me wrong, by showing the way ;-).

Cheers,
_Oz_

Rohit Tripathi

unread,
May 27, 2012, 10:03:54 AM5/27/12
to personal...@googlegroups.com
On Sun, May 27, 2012 at 7:05 PM, Sean <xux...@gmail.com> wrote:
> is there any way to list down
> all variables in debug mode?

My favorite question (because I have an answer)!

Try the following code, in table "t" you'll have almost everything.

gettyp:{ t:(`mixed`short`int`float`char`symbol`table`dict`func`unary_primitive`binary_primitive`projection`composition`f_each`f_over`f_scan`f_each_prior)!(0h;
5h; 6h; 9h; 10h; 11h; 98h; 99h; 100h; 101h; 102h; 104h; 105h; 106h;
107h; 108h; 109h) ;

typ:first where t=abs x;
$[0>x;` sv `atomic,typ;99h>abs x;` sv `list,typ;typ]};

t:raze { id:raze ` sv'x,/:key x;
tbl:flip `id`typ`def!($[x~`.q;key x;id];type each value each
id;get each id);
tbl,raze .z.s'[exec id from tbl where typ=99h]
}'[` sv'`,'key `];

Peter Byrne

unread,
May 27, 2012, 10:32:50 AM5/27/12
to personal...@googlegroups.com
to show the values of the arguments and locals in a function you could access them from the .z.s definition and use eval to get their values:

q)f:{[x;y;z]a:100;break;b:1}
q)f[1;`a;1 2]
{[x;y;z]a:100;break;b:1}
'break
q))show raze{enlist[x]!enlist eval x} each raze value[.z.s]1 2
x| 1
y| `a
z| 1 2
a| 100
b| ()


Oleg Zakharov

unread,
May 27, 2012, 10:40:36 AM5/27/12
to personal...@googlegroups.com

Thank you, Peter!
eval will help where value wouldn't, indeed.

Glad to be wrong :)

Aaron Davies

unread,
May 28, 2012, 8:52:23 PM5/28/12
to personal...@googlegroups.com
> eval will help where value wouldn't, indeed.
>
>> to show the values of the arguments and locals in a function you could access them from the .z.s definition and use eval to get their values:
>>
>> q)f:{[x;y;z]a:100;break;b:1}
>> q)f[1;`a;1 2]
>> {[x;y;z]a:100;break;b:1}
>> 'break
>> q))show raze{enlist[x]!enlist eval x} each raze value[.z.s]1 2
>> x| 1
>> y| `a
>> z| 1 2
>> a| 100
>> b| ()

note that this only works in a debug shell

q){eval`x}1
{eval`x}
'x
@
![-6]
`x
q))eval`x
1
q))

but in the debug shell, it even works across scopes (as seen above)!

q){a:1;eval`a}[]
{a:1;eval`a}
'a
@
![-6]
`a
q))eval`a
1
q)){eval`a}[]
1
q))

interestingly `.z.s binds to the outer function here, not the inner one:

q){break}[]
{break}
'break
q)){eval`.z.s}[]
{break}
q))

using these points, i'd write

q)vars:{show n!eval each n:raze get[eval`.z.s]1 2}

then

q)f:{[x;y;z]a:100;break;b:1}
q)f[1;`a;1 2]
{[x;y;z]a:100;break;b:1}
'break
q))vars[]
x| 1
y| `a
z| 1 2
a| 100
b| ()
q))

note that "uninitialized" locals have the value (), which may help give a rough idea of where in the function you've broken
Reply all
Reply to author
Forward
0 new messages