Programming and Scripting :: Lua functions and other bits



Here's a nice collection of Lua code snippets and functions:
http://lua-users.org/wiki/SampleCode

Some work as-is in murgaLua, and some need a little modification. For example, the length_of_file function on the "File Input Output" page uses assert, which crashes murgaLua if the file doesn't exist instead of just giving an error message. I used "if" instead to check if io.open worked:
Code Sample
function length_of_file(filename)
 local fh = io.open(filename, "rb")
 if fh then
 local len = fh:seek("end")
 fh:close()
 return len end
end

The original function was written by Jay Carlson, the creator of Lua-FLTK

If anyone else knows of a good resource for Lua samples, please paste a link here. thanks.
-----------------------------

Here are a couple I did yesterday while working on another project (murgaLua only):

-- Returns a hex value from the given widget's color.
-- In the case of redthing:color(fltk.FL_RED)
-- hexcolor(redthing) returns #FF0000
Code Sample
function hexcolor(widget)
       local r,g,b=Fl:get_color(widget:color(),r,g,b);
       local hex=string.format("#%.2X%.2X%.2X",r,g,b);
       return hex
end


-- An alternative to the above, which uses a color index (0-255) as
-- an argument. You can plug in any FLTK color rather than rely on
-- a specific widget's base color. Numbers above 255 will work, but
-- I couldn't tell you what rgb values they represent =o)
Code Sample
function hexcolor2(num)
       local r,g,b=Fl:get_color(num,r,g,b);
       local hex=string.format("#%.2X%.2X%.2X",r,g,b);
       return hex
end

You can see my collection of generic Lua functions that I have been building as I write Lua scripts for DSL in /etc/init.d/functions5.lua

I also have created many generic non-GUI Lua scripts in DSL. I lost count. I usually factor out code and I place it in my functions5 collection.

I think DSL has more use of LuaFltk and generic stand-a-lone Lua scripts than most any other place.

I still write Lua4 as well Lua5 and even have Lua on my FreeDOS diskette.

Lua is a small, very fast, and a very nice language.

I know, you probably can't find many jobs as a Lua script writer and most sites use Lua together with C.

I prefer John Murga's bindings as they are much closer to the standards of Lua and Fltk.  The prior version, Jay Carlson's Fltk binding seemed to have a tcl-like parameter passing scheme.

Anyway, it is a nice language. You can find good documentation for Lua5.1 and most of the Fltk Manual is also helpfful for the GUI side.

I think DSL has nearly 60 Lua programs.

Honestly, I would rather code Lua than the other tasks that I do for DSL.



I knew of /etc/init.d/functions.lua, but I didn't even think to look at functions5.lua...sometimes I get it into my head that a particular file is probably just a backup or test version that the author forgot to delete. There are so many of them floating around in various archives that i usually don't bother looking in them unless i'm having trouble with the archive.  Anyway, yeah, that's something i want to study.

I also am reading through the tutorial pages at lua-users.org. They're not really tutorials by my definition, and most of them cover the same material as the Lua manual, but they use real  examples rather than that friggin annoying BNF syntax. So the tutorials are a definite plus.
(no, I don't believe BNF has ever helped me to translate between languages...it has only added a second layer of translation that I have to deal with)

Any idea how something like this might be translated to work in murgaLua?
Code Sample
-- Perform a shell command and return its output
function shell(c)
 local o, h
 h = assert(io.popen(c,"r"))
 o = h:read("*all")
 h:close()
 return o
end
print(shell("ls"))


It works in Lua 5.1, but apparently popen is not implemented in murgaLua.
Until now I just assumed that shell command output needed to be redirected to a file first and then read into Lua.  I hope it's not the case with Murga.

Apparently popen was not compiled into murgaLua from Lua source.
Interestingly, it is also the case for Carlson's Lua4 bindings as well.
That is why I wrote my own capturing function. But like you stated it uses temp files.

Next Page...
original here.