Daniel Krizian
Daniel Krizian

Reputation: 4716

Assign multiple local variables by name

How can I assign multiple local variables by names?

For example, within a lambda, something like this:

{
 @[;:;] (.') flip (`a`b;4 2);
 (a;b)
 }[] / should return 4 2

But obviously does not work, because

q)@[`a;:;4]                                                                                   
'type

Also,

q):[`a`b;4 2]
'assign

and using set:

q)set'[`a`b;4 2]

assigns to global, not local environment.

Upvotes: 2

Views: 523

Answers (1)

Fiona Morgan
Fiona Morgan

Reputation: 520

I believe you'll have difficulty saving local variables this way without a hacky solution and without knowing the reason for it needing to be local it's hard to produce a solution that could help. However, here's some suggestions:

  1. if just you don't want global variables at the end of execution you could save these as global and remove them before exiting
  2. if you don't want existing globals overwritten then I suggest saving these variables in a local dictionary instead with variable names as the key. eg. (!). (`a`b;4 2)

You can access local variables using

q){c:3;?[(`$())!();();0b;`c]}[]
3

but the problem here is the assigning of values to local variables.

Upvotes: 2

Related Questions