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
7 years ago
function ToucanBeak(source, find, replace, wholeword)
  if wholeword then
    find = '%f[%a]'..find..'%f[%A]'
  end
  return (source:gsub(find,replace))
end

function WaveBeak(message,player)
local newmessage1 = string.lower(message)
local newmessage2 = ToucanBeak(newmessage1, "teemo", "warbler", false)
local newmessage3 = ToucanBeak(newmessage2, "teemoing", "warbler maining", false)
end

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
7 years ago
Edited 7 years ago

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

You could use a list:

local simple = {
    {from = "teemo", to = "warbler"},
}
local whole = {
    {from = "foo", to = "bar"},
}
for _, rule in pairs(simple) do
    message = ToucanBeak(message, rule.from, rule.to, false)
end
for _, rule in pairs(whole) do
    message = ToucanBeak(message, rule.from, rule.to, true)
end

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

local simple = {
    teemo = "warbler",
}
local whole = {
    foo = "bar",
}

for from, to in pairs(simple) do
    message = ToucanBeak(message, rule.from, rule.to, false)
end
for from, to in pairs(whole) do
    message = ToucanBeak(message, rule.from, rule.to, true)
end
0
I take it you meant "end" instead of "edn", but also well said. TheHospitalDev 1134 — 7y
0
Thank you! 3dsonicdx 163 — 7y
Ad

Answer this question