lua-users home
lua-l archive

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


Hi all,
My project is coming along nicely, but one thing that keeps nagging me is my horrible collection of support functions, mostly for handling strings. I'm posting in the hope that others will provide me with code examples/functions.
What I need the most is tokenizing functions, for trimming leading/trailing spaces off a string, and removing words one at a time.

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:
<pre>

function ssub(string, m1, m2)
  local x = function(c)
    -- c should be two letters, split it up
    local a, b = strsub(c, 1, 1), strsub(c, 2, 2)
    -- use m1 for replacements
    if a == "s" then
      if b == "n" then
        return %m1.name
      elseif b == "N" then
        return cap(%m1.name)
      elseif
        ...
    -- use m2 for replacements
    elseif a == "t" then
      if b == "n" then
        ...
    end
  end

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

</pre>

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?

==
|    ._  _   Lune MUW Server
|_(_)| )(/_  http://lune.sourceforge.net/

_____________________________________________________________