Hi,In function f I need to use to value to evaluate dynamic strings wheresome variables are embedded, however, I find it’s not possible toaccess the function’s local variables inside the evaluation, thoughglobal variables are ok. For example,q)f:{a:100;value “a”}q)f{a:100;value “a”}'a.:"a"q))a100q))value "a"100q))g:{b::200;value “b”}q))g200 I don’t want to have too many global vars. Are there some solutions?Thanks a lot!
If you need to use dynamic strings you can just inline the values intothe strings:q)f:{a:100;value string a}q)f100q)g:{a:100;value “5+”,string a}q)g105But 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.jason>:> 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!>> –>
Submitted via Google Groups</cl.jason>
value string a is different from value "a"the reason I need dynamic string is because I need to construct somequeries dynamically where some terms are local variables generated atrun-time, e.g.:\l sp.qq)f:{slocal:select s,city from s; v:value “select count s by cityfrom slocal”,many other where conditions;…many other processing of vand 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, thereare issues of visibility of global/local vars
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.qFor this kind of work you should use functional select.https://code.kx.com/trac/wiki/QforMortals2/queries_q_sql#FunctionalselectHTHKim>> 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>
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.davies@gmail.com
that’s one problem bugging me for long, is there any way to list downall variables in debug mode?q)f{a:100;break}q)f{a:100;break}'breakq))\vsymbol$()q))key
.,`fq))q))On May 27, 3:26?am, Aaron Davies <aaron.dav…> wrote:> > 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.dav...@gmail.com</aaron.dav…>
On Sun, May 27, 2012 at 7:05 PM, Sean 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
shortint
floatchar
symboltable
dictfunc
unary_primitivebinary_primitive
projectioncomposition
f_eachf_over
f_scanf_each_prior)!(0h;5h; 6h; 9h; 10h; 11h; 98h; 99h; 100h; 101h; 102h; 104h; 105h; 106h;107h; 108h; 109h) ; typ:first where t?s x; $[0>x;
sv atomic,typ;99h>abs x;
sv list,typ;typ]};t:raze { id:raze
sv’x,/:key x; tbl:flip id
typdef!($[x~
.q;key x;id];type each value eachid;get each id); tbl,raze .z.s’[exec id from tbl where typ?h] }‘[` sv’`,'key `];
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_
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!enlist eval x} each raze value[.z.s]1 2
x| 1
y| `a
z| 1 2
a| 100
b| ()
Thank you, Peter!
eval will help where value wouldn’t, indeed.
Glad to be wrong :)
To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1084)
> 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){evalx}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;evala}[] {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=