Recent Changes - Search:

PmWiki

pmwiki.org

edit SideBar

Lua

Lua is a small scripting language that can be embedded into larger systems.


Code fragments
How to get Lua to allow direct binary number input.

setmetatable(_G, {__index = function(_,k) if type(k) == "string"
and k:match"^Ob[01]+$" then return tonumber(k:sub(3), 2) end end})

print(Ob11111111)  --> 255

Set up to check for nil parameters being passed.


local function arguments(args)
	if next(args) == nil then print"no arguments, did you forget something?" end
	return next, args
end
= You can then use this as follows:
for k, v in arguments(named_args) do -- this will magically print the error message if the argument table is empty
	print(k, v)
end

I have just finished rewroting the documentation of eoo.lua, that is a very minimalistic implementation of OO in Lua in just 5 lines of code. Its code is just this, except for some functions that I only use very rarely:

Class = {

      type   = "Class",
      __call = function (class, o) return setmetatable(o, class) end,
    }

setmetatable(Class, Class)

Most people will probably think that this has too few features to be useful to them, but the diagrams in the documentation are very nice, and can be adapted to other implementations of OO too (I guess).

It is here:

http://angg.twu.net/LUA/eoo.lua.html http://angg.twu.net/LUA/eoo.lua

Cheers, Eduardo Ochs http://angg.twu.net/luaforth.html


Info about using Zero Brane? Studio You can check lua54 branch of Zero Brane? Studio, which already includes compiled luasocket/luasec modules for Lua 5.4 on Windows ( https://github.com/pkulchenko/ZeroBraneStudio/tree/lua54). It also provides the Lua interpreter itself, but you may want to replace it with a newer version if needed.


Lua https library I recently used this one[0,1] for the first time (my first serious Lua project, and first time doing anything HTTP with Lua). Managed to quickly get things done with it without too much fuss. It's also 100% Lua I think, so you won't have any problems compiling!

[0]: https://luarocks.org/modules/daurnimator/http [1]: https://github.com/daurnimator/lua-http and also Lua Sec?, it also works for Windows.

We embed a light http library by Steve Kemp. Details in the attachment, exported from the Note Case? Pro Help file.


Lua RT? 0.9.9 is now available, and can be downloaded at https://www.luart.org. Lua RT? is a multipurpose programming framework with an optimized Lua runtime library for Windows, based on Lua 5.4.4.


Windows development environment Visual Studio Code together with Sumneco Lua extension


How to do type conversions in a function Given a function that takes numbers, how can I get it to also accept strings

function incr(n) if type(n) == "string"

    n = tostring(tonumber(n) + 1;

elseif type(n) == "number"

    n = n + 1;

else

    print type(n);

end end


On 21 September 2012 15:34, Rena <hyperhacker@gmail.com> wrote: > (Yep, this question again. :p) > > I'm coding in a rather limited Linux-like environment: I have a text editor, > a terminal emulator, a Lua 5.1 interpreter, and not much else. I don't have > a C compiler. > > My problem is I want to read keys from stdin as soon as they're pressed, > without having to press enter. The only ways I know to do this involve C > function calls. Is there perhaps an ANSI escape code or some other crazy > trick I can use? Can I call some program with os.execute to change terminal > settings?

Try this small function:

    function getchar(n)
        local stty_ret = os.execute("stty raw 2>/dev/null");
        local ok, char;
        if stty_ret == 0 then
            ok, char = pcall(io.read, n or 1);
            os.execute("stty sane");
        else
            ok, char = pcall(io.read, "*l");
            if ok then
                char = char:sub(1, n or 1);
            end
        end
        if ok then
            return char;
        end
    end

An improvement would be to save the current stty settings, and then restore them (instead of 'stty sane'). I don't know how "portable" this is though.


Bundle Lua to make it portable srlua: https://web.tecgraf.puc-rio.br/~lhf/ftp/lua/#srlua

For a relevant recent thread, see http://lua-users.org/lists/lua-l/2021-07/msg00086.html


Links to cool things to do with Lua

Edit - History - Print - Recent Changes - Search
Page last modified on April 26, 2024, at 02:44 AM