2

How can I get value to see a variable in a local scope? For example:

a:2;

func:{
  a:1;
  value "a"
 }

returns 2

Donald_W
  • 1,773
  • 21
  • 35
nightTrevors
  • 639
  • 4
  • 11
  • 24
  • 1
    You shouldn't ever need to use value (or even eval) like this. There would be better/cleaner ways to do it. A more comprehensive real-life example might help – terrylynch Feb 05 '15 at 15:20
  • I have a function that takes a list of functions that update new columns onto a table. The cleaner way to do it is with a functional update, but I came across this issue while finding an alternative to that very ugly syntax. – nightTrevors Feb 05 '15 at 19:45
  • 1
    Some people would say that that very ugly syntax is beautiful! But sometimes functional form is just necessary, and arguably less ugly than generating stringified lines of code to be evaluated. One way to use functional form while still retaining some user-friendly readability is to use something similar to Aaron Davies' "semi-functional" forms. See http://www.q-ist.com/2012/10/functional-query-functions.html and http://www.q-ist.com/2013/03/my-kdb-user-meeting-presentation.html – terrylynch Feb 05 '15 at 22:00
  • Absolutely :) I use functional select in my rdb/hdb gateway process, but in this particular instance, it was easier just to stringify a little bit of code, and then I got to learn a little more about value by posting this question! Thanks for sharing Aaron's articles, I've referenced many things he has posted – nightTrevors Feb 05 '15 at 23:36

2 Answers2

2

value will always work on global scope.

If you really need this, maybe make use of workspace variables, e.g. .a.b:1 ... I don't have a q instance to hand to test if that works but i'm almost sure it does.

Manish Patel
  • 4,411
  • 4
  • 25
  • 48
0

You can use other function instead of 'value'. One option is 'eval' function:

        q)a:2;

        q) func:{ a:1; eval a}
        q)  func[]
        q) 1
Rahul
  • 3,914
  • 1
  • 14
  • 25