Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion Goto page Previous  1, 2
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

PostPosted: Sat May 30, 2009 9:39 pm   
 
I get the same error.
Code:
string.gsub(string, "RED", zs.var.names.RED)
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Sat May 30, 2009 10:14 pm   
 
You didn't change the name of the variable "string" like I said to.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
cosine_omerta
Wanderer


Joined: 14 Oct 2007
Posts: 50

PostPosted: 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)
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: 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.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
cosine_omerta
Wanderer


Joined: 14 Oct 2007
Posts: 50

PostPosted: 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)
Reply with quote
wrym
Magician


Joined: 06 Jul 2007
Posts: 349
Location: The big palace, My own lil world

PostPosted: 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
Reply with quote
cosine_omerta
Wanderer


Joined: 14 Oct 2007
Posts: 50

PostPosted: 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)
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net