Difference between revisions of "Lua Snippets"

(added snippet)
(STRING)
 
Line 11: Line 11:
 
local age = tonumber(string.match(mystring, "%d+"))
 
local age = tonumber(string.match(mystring, "%d+"))
 
print(age) -- outputs 26
 
print(age) -- outputs 26
 +
</source>
 +
 +
=== Check if string X starts or ends with string Y '''@lua-users wiki''' ===
 +
<source lang="lua">
 +
local function starts_with(str, start)
 +
    return str:sub(1, #start) == start
 +
end
 +
 +
local function ends_with(str, ending)
 +
    return ending == "" or str:sub(-#ending) == ending
 +
end
 
</source>
 
</source>
  

Latest revision as of 04:52, 29 June 2020

Here are some code snippets for the Lua language.
All samples are ready to use. Enjoy!

STRING

FINDING A NUMBER IN A STRING @xxx

local mystring = "Doe is 26 years old."
local age = tonumber(string.match(mystring, "%d+"))
print(age) -- outputs 26

Check if string X starts or ends with string Y @lua-users wiki

local function starts_with(str, start)
    return str:sub(1, #start) == start
end

local function ends_with(str, ending)
    return ending == "" or str:sub(-#ending) == ending
end

TABLE

ADDING 2 TABLES @xxx

method 1:

z = {}
n = 0
for _,v in ipairs(x) do n=n+1; z[n]=v end
for _,v in ipairs(y) do n=n+1; z[n]=v end

method 2:

levelsetup = {color=0x5E3C3A, isshape=true, restitution=0, friction=1}
tablebase = { x = 64, y = 200, w = 400, h = 250, rotation = 0}
for k, v in pairs(levelsetup) do
	tablebase[k] = v
end









More to come God's willing...