Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: thespindoctor on March 24, 2018, 09:31:04 AM

Title: Lua file operations
Post by: thespindoctor on March 24, 2018, 09:31:04 AM
Hoping for some help on basic file operations in Lua.

Is there a reference for how to modify the code for file operations from the lua.org/manual information to get it to work in Mach4Lua?
It does not seem to recognize the io.write and io.code but recognized io.open

Here is the simple version I am trying.

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

stuff="--one,two,three"

filetoget=path .. "\\testfile.csv"

io.output=io.open(filetoget,"a+")
io.write(stuff)
io.close()

Thanks!
Title: Re: Lua file operations
Post by: arhimedoffs on March 24, 2018, 10:24:23 AM
Just correct
Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

local stuff="--one,two,three"

local filetoget=path .. "\\testfile.csv"

io.output(io.open(filetoget,"a+"))

io.write(stuff)
io.close()

* local not requied, but recommended, instead global variable created
Title: Re: Lua file operations
Post by: Chaoticone on March 24, 2018, 11:15:23 AM
This might help.

http://www.machsupport.com/forum/index.php/topic,35896.msg246530.html#msg246530
Title: Re: Lua file operations
Post by: thespindoctor on March 24, 2018, 11:31:41 AM
so the correction was successfu!

file:write and file:close etc not just io.write

Thanks!

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

local stuff="--one,two,three"

local location=path .. "\\testfile.csv"
wx.wxMessageBox(location)
file = io.open(location,"a+")
file:write(stuff)
file:close()
Title: Re: Lua file operations
Post by: thespindoctor on April 05, 2018, 08:45:02 AM
How do I write to a new line?

I tried


file = io.open(FileToUse, "a+")   
file:write("test\n")
file:write("test2\n")

but it adds it to the same line

I get
test/ntest/n and I want

test
test

Thanks
Title: Re: Lua file operations
Post by: TPS on April 05, 2018, 10:01:50 AM
without any knowledge of lua, i would say


File:write("test","\r\n")
Title: Re: Lua file operations
Post by: joeaverage on April 06, 2018, 03:57:56 PM
Hi,
this is so helpful it lives on my desktop when coding:

https://www.lua.org/manual/5.2/ (https://www.lua.org/manual/5.2/)

The section you want is 6.8

Craig
Title: Re: Lua file operations
Post by: thespindoctor on April 07, 2018, 05:41:18 AM
Hi,
this is so helpful it lives on my desktop when coding:

https://www.lua.org/manual/5.2/ (https://www.lua.org/manual/5.2/)

The section you want is 6.8

Craig

Thanks Craig!  Sure appreciate you always trying to help!  I also spent lots of time on that site and have the physical book.  My problem has been that the methods in the lua docs sometimes don't work with Mach4 Lua.  The conventions are enough different that I spend hours with trial and error to finally come up with the sometimes subtle difference that is finally successful.  

I did learn that issuing a file:write('\n') command moves to the next line in the file to write.  At first it would not work and I don't know why.  Sometimes file writing works and sometimes it does not.  It could be that a file is left open when a bug occurs and knocks the program out before completing the close.  Sometimes restarting fixes the problem

The last pearl I discovered was from a post on another of several websites that teach lua or respond to problems.  Formatting numbers has been tricky for me.  I get long floating point numbers from the analog voltage inputs and using variable=tostring(math.floor(analogvalue+.5) worked great to round the number properly and convert it to an integer.  I could not figure out how to get an integer string from the lua docs.  Lots of trial and error to pick at each detail and technique is the only way to be successful with lua.  wx.widgets is even harder.  

Thankful for the forum!!

Keith
Title: Re: Lua file operations
Post by: thespindoctor on April 07, 2018, 06:01:43 AM
Here is another site with many more examples to expand on the Lua.org

http://lua-users.org/wiki/
Title: Re: Lua file operations
Post by: joeaverage on April 07, 2018, 05:38:21 PM
Hi Keith,

Quote
My problem has been that the methods in the lua docs sometimes don't work with Mach4 Lua
I haven't encountered that. Certainly Mach4 uses version 5.2 and there are differences between 5.1 and 5.3 which will trip you up, I had a problem because I was looking at 5.3 docs and took me a while
to figure out why nothing worked.

The other thing is that all of chapter 6 in the 5.2 docs is about Lua 'Standard Libraries'. So all the file functions you are experimenting with are a library, not core Lua. The Library is constructed as a table.
In this case the table is called 'io'.  io.open  and io.close  and io.flush are all entries of the table io, the entry is a function which is a very clever use of the principle of 'functions as a first class values'.
Given that a library is an addon its quite possible that certain features don't work as advertised although I haven't found any which don't comply to 5.2 docs.

Craig
Title: Re: Lua file operations
Post by: Chaoticone on April 08, 2018, 12:46:03 AM
I would create a single string then write that single string to the file once. You're opening once and writing twice.

A perfectly working example can be found here.
http://www.machsupport.com/forum/index.php/topic,35896.msg245756.html#msg245756