Received: by 10.150.87.2 with SMTP id k2mr928997ybb.26.1254861495940; Tue, 06 Oct 2009 13:38:15 -0700 (PDT)Date: Tue, 6 Oct 2009 13:38:15 -0700 (PDT)X-IP: 65.199.113.40User-Agent: G2/1.0X-Google-Token: BuFkVQwAAAAVXTllMZ0nhxEZ88RUMjh8X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729),gzip(gfe),gzip(gfe)Message-ID: Subject: create table inside of functionFrom: Q Expert To: “Kdb+ Personal Developers” X-Google-Approved: simon.garland@gmail.com via web at 2009-10-07 09:50:53How does one go about creating a table inside of a function and thenreturning it?I tried the following:q)createTable:{testTable:(x:()); testTable insert (3); testTable}q)createTable[]{testTable:([]x:());
testTable insert (3); testTable}'typeinsert`testTable3unless I define testTable in the global scope, at which point theinsert seems to work:q)testTable:(x:())q)createTablex-so, function executes, apparently insert works, but an empty table isreturned.Looking at the global testTable however:q)testTablex-3so, it’s inserting into the global table instead, how do I get aroundthis? I tried:q)createTable2:{testTable2:(x:()); testTable2.x ,: 3; testTable}q)createTable2{testTable2:(x:()); testTable2.x ,: 3; testTable}'testTableandq)createTable3:{testTable3:(x:()); testTable3.x ,: 3}q)createTable3q)testTable3 | ::x| ,3so apparently now, I’m creating some sort of an object from inside ofthe function, but that is only available from the outside scope. Isthere any explanation for any of this?Thanks in advance.
X-Mailer: Apple Mail (2.936)On Oct 7, 2009, at 4:38 AM, Q Expert wrote:> q)createTable:{testTable:(x:()); testTable insert (3); testTable}insert, like any other call-by-name function in q, only works on the global scope\> q)createTable3:{testTable3:([]x:()); testTable3.x ,: 3}\> q)createTable3[]\> q)testTable3\> | ::\> x| ,3\>\> so apparently now, I'm creating some sort of an object from inside of\> the function, but that is only available from the outside scope. Is\> there any explanation for any of this?the correct way to do this isq)createTable4:{testTable4:([]x:()); testTable4 ,: enlist 3;testTable4}q)createTable4[]x-3note that in this case, at least, ",:" has no return value, so you have to return the table explicitly.as to why your createTable3 created an object in global scope, i'm not entirely sure. from the structure, it looks like a pseudo-directory; you can get metadata on the functionq)get{testTable3:([]x:()); testTable3.x ,: 3}0x0ba152a05021031602a20b04810c0004,
x,testTable3``testTable3.x,
xplist3"{testTable3:(x:()); testTable3.x ,: 3}"and see that it’s interpreted “testTable3.x” as a reference to a global. (the order is (bytecode;parameters;locals;globals;constants…;sourcecode))