lua-users home
lua-l archive

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


 > What I need the most is tokenizing functions, for
 trimming leading/trailing spaces off a string, and removing words one at a
 time.

How about the following:

-- strip leading and trailing white space from a line
function strip(l)
    return gsub(l, "%s*(.*)%s*", "%1", 1)
end

Actually, this wouldn't work. The trailing spaces are captured. Try:

     return gsub(l, "%s*(.-)%s*$", "%1", 1)

-- remove a prefix p from s
-- return the beheaded s and the removed prefix
function behead(s, p)
    local s2, z

    z, z, p, s2 = strfind(s, p .. ")(.*)")

There's an open-parenthesis missing:

    z, z, p, s2 = strfind(s, "(".. p .. ")(.*)")

Otherwise, they work great.

I also had a problem with upvalues. I was sure I was using them correctly, but Lua kept telling me that I can't access locals out of scope... my code looked something like this:
(...)
  string, junk = gsub(string, "{.-}", x)
(...)

It takes a two-letter code, surrounded by braces, and replaces it with the appropriate strings from either object passed to ssub(). This seems to be exactly like the example given in the manual in the section for upvalues, but here it just doesn't seem to work?

As far as the upvalues are concerned, your code worked fine for me. The only problem I detected so far was your gsub pattern. It should be:

  string, junk = gsub(string, "{(.-)}", x)

Regards,

	Pedro.
--
Pedro Miller Rabinovitch
Gerente Geral de Tecnologia
Cipher Technology
www.cipher.com.br