Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I simplify this so I don't make a thousand local variables?

Asked by
3dsonicdx 163
8 years ago
01function ToucanBeak(source, find, replace, wholeword)
02  if wholeword then
03    find = '%f[%a]'..find..'%f[%A]'
04  end
05  return (source:gsub(find,replace))
06end
07 
08function WaveBeak(message,player)
09local newmessage1 = string.lower(message)
10local newmessage2 = ToucanBeak(newmessage1, "teemo", "warbler", false)
11local newmessage3 = ToucanBeak(newmessage2, "teemoing", "warbler maining", false)
12end

Basically ToucanBeak just subs the target word out for something out.

WaveBeak sets everything to lowercase, and then uses ToucanBeak to get the new message.

I intend to do a lot more with this - is there any way I can make it more efficient/use way less local variables?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

Use a table. When you find yourself numbering variables, it means you just want a list.

You could use a list:

01local simple = {
02    {from = "teemo", to = "warbler"},
03}
04local whole = {
05    {from = "foo", to = "bar"},
06}
07for _, rule in pairs(simple) do
08    message = ToucanBeak(message, rule.from, rule.to, false)
09end
10for _, rule in pairs(whole) do
11    message = ToucanBeak(message, rule.from, rule.to, true)
12end

Also, by the nature of "find/replace" you could just use a dictionary:

01local simple = {
02    teemo = "warbler",
03}
04local whole = {
05    foo = "bar",
06}
07 
08for from, to in pairs(simple) do
09    message = ToucanBeak(message, rule.from, rule.to, false)
10end
11for from, to in pairs(whole) do
12    message = ToucanBeak(message, rule.from, rule.to, true)
13end
0
I take it you meant "end" instead of "edn", but also well said. TheHospitalDev 1134 — 8y
0
Thank you! 3dsonicdx 163 — 8y
Ad

Answer this question