lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> the best i have found for parsing real numbers is this:
[..snip..snip..]
> but i still don't know how to use it with strfind()

Regular expressions in standard Lua do not support branches and optional
parts of more than a single character, so you have to break up complex
expressions into several parts.  Of course you can use my PCRE module (see
http://lua-users.org/lists/lua-l/2003-05/msg00279.html) to support full Perl
compatible regular expressions if you're using Lua 4.0.1  (Or Rueben Thomas'
POSIX based rex module for Lua 5).  Then you could do things like:

    > p = pcre.compile("(\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE]([+-]?\\d+))?",
pcre.ANCHORED)
    > print(p:exec "123.4E17")
    > 1    8    123.4    17
    > print(p:exec ".157e-100")
    > 1    9    .157    -100
    > print(p:exec "123.")
    > 1    4    123.
    > print(p:exec "1e")    -- note that "e" is not matched
    > 1    1    1

> Learning Lua is turning into a bit of a nightmarge,

But worth the trouble!  Best way is to try lots of stuff in an interactive
session...

> is there any way to make a Lua variable reference another variable?

No.  There is no reference type in Lua.  In Lua you reference a value in one
of two ways:

1.  by name (local values, upvalues)
2.  by table index.  This includes all global values (from the "global
environment".)

For both kinds of value refences you can implement "surrogate" references
(using closures and metamethods respectively), but this is probably not what
you're looking for.

In Lua, things are a bit more explicit.  In terms of your example: don't use
a, b as variables, but use the table t = {a = 0, b = 0} instead.  Then
clearly you can simply do t.a, t.b = 10, 20 to modify the values and you can
pass the table t around to whatever function you like.

References (e.g. in C++ or Pascal) are often used to allow side effects in
function calls.  In Lua this can often be replaced by returning multiple
values.  Example:

// C++ example...
bool find_range(const char *str, int &first, int &last)
{
    bool res;
    int f, l;
    // do some computations...
    if (res) { first = f; last = l; }
    return res;
}

In Lua this could become:

function find_range(str)
    local res, first, last
    -- do some computations
    if res then
        return res, first, last
    end
end


Hope this doesn't add to your nightmares...  ;-)

Bye,
Wim