Dear all, I save a table “tfList” using table.save in Lua: \startluacode ... table.save(“tempList.lua”,tfList) ... \stopluacode And I call it in MetaFun using a method explained in the MetaFun book. \startMPcode lua("MP = { } MP.data = table.load('tempList.lua')") ; … \stopMPcode However, I found that whenever I save “tfList”, table contents are appended to the file “tempList.lua” at the end which cause a trouble. I want the tempList.lua contains the new table only. I search the method to do that, but I can’t find it. The only way is to change the file name each time which is not good to make a macro. Is there a way to clear the contents in the tempList.lua before I save another table? Thank you for reading. Best regards, Dalyoung
On 1/11/2025 4:01 PM, Jeong Dal via ntg-context wrote:
Dear all,
I save a table “tfList” using table.save in Lua:
\startluacode ... table.save(“tempList.lua”,tfList) ... \stopluacode
And I call it in MetaFun using a method explained in the MetaFun book.
\startMPcode lua("MP = { } MP.data = table.load('tempList.lua')") ; … \stopMPcode
However, I found that whenever I save “tfList”, table contents are appended to the file “tempList.lua” at the end which cause a trouble. I want the tempList.lua contains the new table only. I search the method to do that, but I can’t find it. The only way is to change the file name each time which is not good to make a macro.
Is there a way to clear the contents in the tempList.lua before I save another table?
so you add to that list? ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Dear Hans, Thanks for the reply.
The only way is to change the file name each time which is not good to make a macro. Is there a way to clear the contents in the tempList.lua before I save another table?
so you add to that list?
I need more explanation. In the document.truthtable(), it saves the truth table of the defined logic function to the file “tempList.lua” table.save("tempList.lua", tfList) Just after that I run \processMPbuffer[Kmap_***] In whch lua("MP = { } MP.data = table.load('tempList.lua')"); calls the contents of the tempList. I did this process several times using the different logic function as following. I want that the file “tempList.lus” contains the current truth table value only not appending it to the last truth table value. So I’d like to clear the contents of the tempList.lua after loading it in MetaFun or before calling table.save("tempList.lua", tfList) Of the new truth table tfList. I made a working example here. \startMPdefinitions numeric u,gap; numeric uk, wd, ht; % numeric tVal[][],a,b,c; string val[],codeOne[],codeTwo[]; uk := LineHeight; wd := 1.5uk; ht := uk+5pt; def KmapFrameNew(expr first,second) = codeOne[0] := "0"; codeOne[1] := "1"; codeTwo[0] := "00"; codeTwo[1] := "01"; codeTwo[2] := "11"; codeTwo[3] := "10"; % label rows label.top(textext("$" & first & "$"), (-.7wd,0)); label.top(textext("$" & second & "$"), (-.3wd,.5ht)); % drawing lines draw (-1.2wd,0) -- origin -- ((2*(length second)+1)*wd,0); draw (0, 1.1ht) -- origin -- (0,-(2*(length first)+.2)*ht); draw (-wd,ht) --origin; % label the rows if (length first) = 1: for i = 0,1: label.top(textext(codeOne[i]), ((-.7wd, -(i+1)*ht))); endfor; else: for i = 0 upto 3: label.top(textext(codeTwo[i]), ((-.7wd, -(i+1)*ht))); endfor; fi; %label the columns if (length second) = 1: for i = 0,1: label.top(textext(codeOne[i]), ((i+1)*wd,.3ht)); endfor; else: for i = 0 upto 3: label.top(textext(codeTwo[i]), ((i+1)*wd,.3ht)); endfor; fi; setbounds currentpicture to boundingbox currentpicture enlarged 2mm; draw boundingbox currentpicture; enddef; \stopMPdefinitions \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|" a = t end context.starttabulate { p } context.FL() for i=1,#a do context.NC() context(a[i]) end context.NC() context.NR() context.LL() end function document.MakeFooter() context.HL() context.stoptabulate() end local tf = {false, true } local tfList = {} local val = 0 function document.truthTable(a,func) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) do context.NC() context(s and "1" or "0") context.NC() context(t and "1" or "0") context.NC() if #a == 3 then break end context(u and "1" or "0") context.NC() val = func(s,t,u) and "1" or "0" context(val) context.NC() context.AR() table.insert(tfList, val) -- context(#tfList) end if #a == 3 then val = func(s,t) and "1" or "0" context(val) context.NC() context.AR() table.insert(tfList, val) end end end document.MakeFooter() table.save("tempList.lua", tfList) end \stopluacode \startbuffer[tbl:p2q] \startluacode function document.logicF(p,q) return ((not p) or q) end document.truthTable({ "x", "y", "x \\to y" },document.logicF) \stopluacode \stopbuffer \startbuffer[Kmap_p2qR] numeric n,u,k ; lua("MP = { } MP.data = table.load('tempList.lua')") ; lua("mp.print('n := ',\#MP.data)") ; KmapFrameNew("p","q") for i = 0,1: for j = 1,2: label.top(textext(lua("MP.data[" & decimal (2*i+j) & "]")),(j*wd,-(i+1)*ht)); endfor; endfor; draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ; draw unitsquare xyscaled (1.8wd,ht) shifted (.6wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ; \stopbuffer \startbuffer[tbl:ABC] \startluacode function document.logicF(p,q,r) return (not p) and (not(p and r) or q) end document.truthTable({ "A", "B", "C","(\\overbar{AC}+B)\\overbar{A}"},document.logicF) \stopluacode \stopbuffer \startbuffer[Kmap_ABCR] lua("MP = { } MP.data = table.load('tempList.lua')") ; numeric n,k ; %lua("mp.print('n := ',\#MP.data)") ; KmapFrameNew("A","BC") for i = 0,1: k := 1; for j = 1,2,4,3: label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht)); k := k + 1; endfor; endfor; draw unitsquare xyscaled (3.6wd,ht) shifted (.7wd,-1.1ht) withpen pencircle scaled 2pt withcolor .625red ; \stopbuffer \startbuffer[tbl:xyz] \startluacode function document.logicF(p,q,r) return (not q) and (p or r) end document.truthTable({ "x", "y", "z","f(x,y,x)"},document.logicF) \stopluacode \stopbuffer \startbuffer[Kmap_xyzR] lua("MP = { } MP.data = table.load('tempList.lua')") ; numeric n,k ; KmapFrameNew("x","yz") for i = 0,1: k := 1; for j = 1,2,4,3: label.top(textext(lua("MP.data[" & decimal (4*i+j) & "]")),(k*wd,-(i+1)*ht)); k := k + 1; endfor; endfor; draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ; draw unitsquare xyscaled (1.8wd,1ht) shifted (.6wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625red ; \stopbuffer \starttext \startplacetable[] {\startcombination[distance=1.5cm,2*1] {\getbuffer[tbl:p2q]}{진리표} {\processMPbuffer[Kmap_p2qR]}{K-map} \stopcombination} \stopplacetable \startplacetable[] {\startcombination[distance=1.5cm,2*1] {\getbuffer[tbl:ABC]}{진리표} {\processMPbuffer[Kmap_ABCR]}{K-map} \stopcombination} \stopplacetable \startplacetable[location=here,reference=tbl:Fxyz] {\startcombination[distance=1.5cm,1*2] {\getbuffer[tbl:xyz]}{} {\processMPbuffer[Kmap_xyzR]}{} \stopcombination} \stopplacetable \stoptext
Dear Hans, I found the problem. I tested the code to give the different file name for each process, but there is no changes. Hence, it is not the problem of the file “tempList.lua” which contains the list “tfList”. It is the problem of the “tfList”. Since I put local tfList = {} before the "function document.truthTable(a, func)”, and save it to tempList.lua. That is, I appended the truth value to the pervious “tfList”! There was no initialization of the tfList ={} By moving “local tfList = {}” to inside the function, the outputs are correct. Thank you for your concern. Best regards, Dalyoung
Is there a way to clear the contents in the tempList.lua before I save another table?
so you add to that list?
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (2)
-
Hans Hagen
-
Jeong Dal