|
cosine_omerta |
Posted: Sat May 09, 2009 6:27 am
Numeric sorting and output with gag |
|
cosine_omerta Wanderer
Joined: 14 Oct 2007 Posts: 50
|
Posted: Sat May 30, 2009 9:39 pm |
I get the same error.
Code: |
string.gsub(string, "RED", zs.var.names.RED) |
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat May 30, 2009 10:14 pm |
You didn't change the name of the variable "string" like I said to.
|
|
|
|
cosine_omerta Wanderer
Joined: 14 Oct 2007 Posts: 50
|
Posted: Sat May 30, 2009 11:24 pm |
I did but put it back because I thought I did something wrong. I made a new variable newstring, initiated it, and tried it both setting the newstring to the old one and leaving. This is what I have right now, but I've tried a dozen variations:
Code: |
local string = ""
local newstring = ""
for i,v in ipairs(keys) do
if tbl[v] > 0 then
string= string .. v .. " is " .. tbl[v] .. ". "
end
end
newstring = string
string.gsub(newstring, "RED", zs.var.names.RED)
zs.send("say " .. newstring) |
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sun May 31, 2009 12:04 am |
You need to change EVERY use of "string" to stop it ever changing the "string" variable at all. Everywhere it says string it should say newstring, except string.gsub.
|
|
|
|
cosine_omerta Wanderer
Joined: 14 Oct 2007 Posts: 50
|
Posted: Sun May 31, 2009 4:05 am |
Still get the same error: attempt to index field 'names' (a nil value) line 15. Line 15 is the string.gsub line.
Code: |
local newstring = ""
for i,v in ipairs(keys) do
if tbl[v] > 0 then
newstring = newstring .. v .. " is " .. tbl[v] .. ". "
end
end
string.gsub(newstring, "RED", zs.var.names.RED)
zs.send("say " .. newstring) |
|
|
|
|
wrym Magician
Joined: 06 Jul 2007 Posts: 349 Location: The big palace, My own lil world
|
Posted: Sun May 31, 2009 8:53 am |
I think the problem is in the line:
Code: |
string.gsub(newstring, "RED", zs.var.names.RED) |
I don't think that the interface between lua and Zscript is fully comprehending that the "names" variable is a table.
With out additional lines of code you might be able to use zs.var["names"].red or zs.var["names"].value.red
Alternatively...
Code: |
string.gsub(newstring, "RED", zs.func.db("@names","red")) |
This simply calls the %db function which directly assesses the @names variable, and returns the value of key "red". |
|
_________________ "To the engineer, all matter in the universe can be placed into one of two categories: (1) things that need to be fixed, and (2) things that will need to be fixed after you've had a few minutes to play with them" - Scott Adams, The Dilbert Principle |
|
|
|
cosine_omerta Wanderer
Joined: 14 Oct 2007 Posts: 50
|
Posted: Sun May 31, 2009 9:55 am |
Ok I got it working. I want to thank Fang for all of his time and effort helping me solve what I thought would be an easy alteration on the original code post :p Here is the finished code in case somebody else can find a use for it in the future:
Code: |
if zs.numparam < 1 then return "" end
local tbl = zs.param(1)
local keys = {}
for k in pairs(tbl) do
table.insert(keys,k)
end
table.sort(keys,function (a,b) return tbl[a] > tbl[b] end)
local newstring = ""
for i,v in ipairs(keys) do
if tbl[v] > 0 then
newstring = newstring .. v .. " is " .. tbl[v] .. ". "
end
end
newstring = string.gsub(newstring, "RED", zs.var.names.RED)
zs.send("say " .. newstring) |
|
|
|
|
|
|