Reply
 
Thread Tools Display Modes
Old 11-08-05, 09:36 AM   #1
Ego
A Deviate Faerie Dragon
 
Ego's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 15
Lua string parsing

I'm looking for a bit of help with parsing a string in Lua.

The pattern I've got is 4 numeric values separated by commas.
Something that look like "1, 2, 3, 4"

I want to split out each value into 4 separate local variables,
for example a, b, c, d.

It would be nice if the spaces between them were optional,
and something that could handle if less than 4 values are
present.

I haven't learned enough on parsing strings in Lua, so if
anyone can provide some help, I'd appreciate it.
Ego is offline   Reply With Quote
Old 11-11-05, 02:19 PM   #2
Littlejohn
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 90
text = "1, 2, 3, 4"

_, _, a, b, c, d = string.find(text, "(%d+), (%d+), (%d+), (%d+)")

That is probably too finicky for use in the real world -- it won't match if there are extra spaces around the commas for example. This allows any number of spaces:

_, _, a, b, c, d = string.find(text, "(%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*,%s*(%d+)")

That's getting complicated enough that you may want to build up the pattern string.rep:

_, _, a, b, c, d = string.find(text, string.rep("(%d+)%s*,%s*", 3) .. "(%d+)")
Littlejohn is offline   Reply With Quote
Old 11-11-05, 03:18 PM   #3
Littlejohn
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 90
If you're going for something that's quick+easy to enter, you might want to skip the comma separated list idea -- it's kind of programmerish ya know? Here's a little function that just grabs all the numbers out of a string. Some mods seem to be overly picky with chat command syntax. (Flexbar comes to mind...) People might like it if your mod is ultra lazy with the chat syntax.

Code:
function get_numbers(t)
    local w = { }
    for x in string.gfind(t, "(%d+)") do
	table.insert(w, x)
    end
    return w
end

local n = get_numbers("  1 , 2,3,	4 5-6 ")

print(n[1], n[2], n[3]) -- print first 3 numbers
print(table.concat(n, ", ") .. "\n") -- print all the numbers found
Littlejohn is offline   Reply With Quote
Old 11-15-05, 08:02 PM   #4
Ego
A Deviate Faerie Dragon
 
Ego's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2005
Posts: 15
Originally Posted by Littlejohn
If you're going for something that's quick+easy to enter, you might want to skip the comma separated list idea -- it's kind of programmerish ya know? Here's a little function that just grabs all the numbers out of a string. Some mods seem to be overly picky with chat command syntax. (Flexbar comes to mind...) People might like it if your mod is ultra lazy with the chat syntax.
Thanks for the code and advice. I got it working using the gfind() method.
Now just got to package up my mod and get it posted.

And I went with the no commas in the chat command. Although the code
provided will ignore them and other stuff, so I guess it doesn't matter.

Thanks,
Ego is offline   Reply With Quote
Reply

Go BackWoWInterface » Developer Discussions » Lua/XML Help » Lua string parsing

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off