I'm reading some contents about lua and strings and i saw an something intersting :blink:
Its about join vars and strings (concantion)
It will print:
Hello my name is Mock and i am a Bear'.
Allright evebody know this. But has another kind to concanete vars, using string.format
Hello my name is Mock and i am a Bear'.
Same thing...
ITS verry easy, look:
And of ocurse this will print:
Hello my name is Mock and i am a Bear'.
Now lets install it, go to functions.lua ad add this code
And have fun!
ps: This dont work if you use local vars.
Using it on my lua irc bot.
Function by lua.org
Its about join vars and strings (concantion)
Lua:
name = "Mock"
kind = "Bear"
print('Hello my name is '..name..' and i am a '..kind..'.')
Hello my name is Mock and i am a Bear'.
Allright evebody know this. But has another kind to concanete vars, using string.format
Lua:
name = "Mock"
kind = "Bear"
print(string.format('Hello my name is %s and i am a %s.',name,kind))
Same thing...
Now with this function string.expand you can join vars most easy to an string, and you can concanate an nil, or boolena or function, but function will appear like this: function: 00474F18function printf(...)
return print(string.format(...))
end
--- Its an emulation to printf in c by lua.org
ITS verry easy, look:
Lua:
name = "Mock"
kind = "Bear"
print(string.expand('Hello my name is $name and i am a $kind.',name,kind))
Hello my name is Mock and i am a Bear'.
Now lets install it, go to functions.lua ad add this code
Lua:
function string.expand(s)
s = string.gsub(s, "$(%w+)", function (n)
return tostring(_G[n])
end)
return s
end
ps: This dont work if you use local vars.
Using it on my lua irc bot.
[22:44:53] <@Mock> !lua nome = "Mock" animal = "Urso" quer = "Comer" comida = "Salmao" printd(string.expand('Ola eu sou o $nome eu sou um $animal. Eu to afim de $quer um $comida .'))
[22:44:53] <+Bearbot> Lua print: Ola eu sou o Mock eu sou um Urso. Eu to afim de Comer um Salmao .
Function by lua.org
Last edited: