Hello Guest it is April 19, 2024, 06:11:08 PM

Author Topic: Lua file operations  (Read 2829 times)

0 Members and 1 Guest are viewing this topic.

Lua file operations
« 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!
Re: Lua file operations
« Reply #1 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

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Lua file operations
« Reply #3 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()
Re: Lua file operations
« Reply #4 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

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: Lua file operations
« Reply #5 on: April 05, 2018, 10:01:50 AM »
without any knowledge of lua, i would say


File:write("test","\r\n")
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: Lua file operations
« Reply #6 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/

The section you want is 6.8

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Lua file operations
« Reply #7 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/

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
« Last Edit: April 07, 2018, 05:43:35 AM by thespindoctor »
Re: Lua file operations
« Reply #8 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/
Re: Lua file operations
« Reply #9 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
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'