• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

string.expand(s)

Mock

Mock the bear (MTB)
Joined
Jul 29, 2008
Messages
619
Reaction score
106
Location
Brazil
I'm reading some contents about lua and strings and i saw an something intersting :blink:
Its about join vars and strings (concantion)
Lua:
name = "Mock"
kind = "Bear"
print('Hello my name is '..name..' and i am a '..kind..'.')
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
Lua:
name = "Mock"
kind = "Bear"
print(string.format('Hello my name is %s and i am a %s.',name,kind))
Hello my name is Mock and i am a Bear'.
Same thing...
function printf(...)
return print(string.format(...))
end
--- Its an emulation to printf in c by lua.org
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: 00474F18
ITS verry easy, look:
Lua:
name = "Mock"
kind = "Bear"
print(string.expand('Hello my name is $name and i am a $kind.',name,kind))
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
Lua:
function string.expand(s)
    s = string.gsub(s, "$(%w+)", function (n)
        return tostring(_G[n])
        end)
    return s
end
And have fun! :D
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:
Thats pretty sick. I knew about this stuff for c++/VB/C# and shit, didnt know LUA had it as well!

__________________
 
Last edited:
Except he didn't say he didn't make it ;)

From Lua website:
Lua:
function expand (s)
      s = string.gsub(s, "$(%w+)", function (n)
            return _G[n]
          end)
      return s
    end

Thanks
 
Except he didn't say he didn't make it ;)

From Lua website:
Lua:
function expand (s)
      s = string.gsub(s, "$(%w+)", function (n)
            return _G[n]
          end)
      return s
    end

Thanks

in any moment i said:
By mock?
._.
 
Lol, Mock... You are bear and you know how to use PC, write on keyboard, write a scripts? :OOOOOOOOOOOOOOO
 
@colandus
I'm reading some contents about lua and strings and i saw an something intersting
i just bring this function to otserv comunnity, but 1/20 scripters read lua reference manual.
 
Real usage for that? To me it is a simple showoff of how to do a C printf version in Lua.
By the way, the is a real no-usage for that: calling regular expressions without due to lazy concadenation rules. Your CPU usage thanks.
 
Back
Top