|
xekon Apprentice
Joined: 11 Oct 2007 Posts: 154
|
Posted: Thu Apr 30, 2009 3:12 am
Lua Trigger with a Regular expression pattern and "|" "or" opperator in pattern |
Trigger Regular Expression Pattern
Code: |
^\| Name\s+: (.*) \s+\|$ |
Trigger command
Code: |
row.name = string.gsub(zs.param(1),"^%s*(.-)%s*$", "%1") |
This works fine with many many items, however I ran into the following item and others like it that have the "|" in it:
Code: |
| Name : \|/ Bright burning Candle \|/ | |
I could not find a way for it to save zs.param(1) as a string instead it always saves it as a table because of the seperator "|"
I have written an elaborate workaround, it works but it seems like there should be a better way to do this, if anyone could think of a better solution please let me know, here is what i ended up doing:
Trigger Regular Expression Pattern (same pattern)
Code: |
^\| Name\s+: (.*) \s+\|$ |
Trigger command (different command)
Code: |
somevar = zs.param(1)
tempstring = ""
if string.sub(tostring(somevar),1,6) == "table:" then
for _, x in pairs (somevar) do
if tempstring ~= "" then
tstring = tostring(somevar[_])
tempstring = string.format("%s\|%s",tempstring,string.gsub(tstring,"^%s*(.-)%s*$", "%1"))
else
tempstring = tostring(somevar[_])
end
end
print(tempstring)
row.name = tempstring
tempstring = ""
else
row.name = string.gsub(zs.param(1),"^%s*(.-)%s*$", "%1")
end
|
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Apr 30, 2009 11:45 am |
You could always just use table.concat with a separator of | to turn it back into a string, but yes, this seems like a bug.
|
|
|
|
wrym Magician
Joined: 06 Jul 2007 Posts: 349 Location: The big palace, My own lil world
|
Posted: Thu Apr 30, 2009 12:29 pm |
I think thats probably more of an unwanted-feature than a bug. simply based on what i posted on the other forum about stringlists / literal strings, probably a side affect of parameters/local variables not having a type, cmud sees the separator, and assumes it's a list/table.
Is there a specific reason your not doing this code in zscript?
And if it has to be in lua, could alternatively put the variable part trigger of the trigger pattern into a literal string type variable and then use zs.var[name]. |
|
|
|
xekon Apprentice
Joined: 11 Oct 2007 Posts: 154
|
Posted: Thu Apr 30, 2009 9:16 pm |
I am working on a project using sqlite3 and Lua for equipment database.
If you look at techs last post in this thread it kinda shows how to get started. http://forums.zuggsoft.com/forums/viewtopic.php?t=31518
I am more or less finished and I will definitely look at using table.concat, that is a great idea, not sure why I didnt think of that! I decided since most of it was going to be in lua to just go ahead and code it all in lua, plus its good practice at getting better at a programming language that is used in other applications as well, the more experience I can get the better! |
|
|
|
Vijilante SubAdmin
Joined: 18 Nov 2001 Posts: 5182
|
Posted: Fri May 15, 2009 8:59 am |
Set the pattern to directly assign the value into a variable.
Code: |
^| Name%s: &{var}%s|$ |
You can set the variable to StringLiteral, and CMud won't change the typing of it. Setting the variable to UseDefault will make the speed of storing the value into the variable just as fast as the local %1. I think the Lua you would need would then be "row.name=zs.var(var)".
|
|
_________________ The only good questions are the ones we have never answered before.
Search the Forums |
|
|
|
xekon Apprentice
Joined: 11 Oct 2007 Posts: 154
|
|
|
|
|
|