help with string-handling

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

help with string-handling

Jason Clow
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/

_____________________________________________________________


Reply | Threaded
Open this post in threaded view
|

Re: help with string-handling

Reuben Thomas-3
> 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

-- 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 .. ")(.*)")
    return s2 or s, p
end

You can use

s = strip(s)
s, token = behead(s, "%w")

to remove words from a string.

-- 
http://sc3d.org/rrt/ | perfect, a.  unsatirizable



Reply | Threaded
Open this post in threaded view
|

Re: help with string-handling

Pedro Miller Rabinovitch-3
 > 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

Reply | Threaded
Open this post in threaded view
|

Re: help with string-handling

Reuben Thomas-4
> >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)

Oops...

> >-- 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 .. ")(.*)")

Oops again...

I should've put a "not actually run" warning on those...

-- 
http://sc3d.org/rrt/
"Reality is what refuses to disappear when you stop believing in it" -
Philip K. Dick