Lua Bookmarks

Published: 2020-04-22, Updated: 2020-06-24


Checar string se começa com texto

ss1 = "hello"
ss2 = "does not start with hello"
ss3 = "does not even contain hello"

pattern = "^hello"

print(ss1:find(pattern) ~= nil)  -- true:  correct
print(ss2:find(pattern) ~= nil)  -- false: correct
print(ss3:find(pattern) ~= nil)  -- false: correct

Formatar data de agora'%Y-%m-%d %H:%M:%S')

Quantidade de itens de um array ou tamanho de uma string

x={1, 2}
print(string.format("x length=%d, string length=%d", #x, #"123"))

Integer para hexadecimal

print(string.format("%x", 10))

Data Types

There are eight basic types in Lua:


The type nil has one single value, nil, whose main property is to be different from any other value; it usually represents the absence of a useful value


The type boolean has two values, false and true. Both nil and false make a condition false; any other value makes it true.


The type number represents both integer numbers and real (floating-point) numbers.

The type number uses two internal representations, or two subtypes, one called integer and the other called float. Lua has explicit rules about when each representation is used, but it also converts between them automatically as needed (see §3.4.3). Therefore, the programmer may choose to mostly ignore the difference between integers and floats or to assume complete control over the representation of each number. Standard Lua uses 64-bit integers and double-precision (64-bit) floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision (32-bit) floats. The option with 32 bits for both integers and floats is particularly attractive for small machines and embedded systems. (See macro LUA_32BITS in file luaconf.h.)


The type string represents immutable sequences of bytes. Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('\0'). Lua is also encoding-agnostic; it makes no assumptions about the contents of a string.


Lua can call (and manipulate) functions written in Lua and functions written in C (see §3.4.10). Both are represented by the type function.


The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value represents a block of raw memory. There are two kinds of userdata: full userdata, which is an object with a block of memory managed by Lua, and light userdata, which is simply a C pointer value. Userdata has no predefined operations in Lua, except assignment and identity test. By using metatables, the programmer can define operations for full userdata values (see §2.4). Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program.


The type thread represents independent threads of execution and it is used to implement coroutines (see §2.6). Lua threads are not related to operating-system threads. Lua supports coroutines on all systems, even those that do not support threads natively.


Tables are the sole data-structuring mechanism in Lua; they can be used to represent ordinary arrays, lists, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing as syntactic sugar for a["name"]. There are several convenient ways to create tables in Lua (see §3.4.9).


As will be discussed in §3.2 and §3.3.3, any reference to a free name (that is, a name not bound to any declaration) var is syntactically translated to _ENV.var. Moreover, every chunk is compiled in the scope of an external local variable named _ENV (see §3.3.2), so _ENV itself is never a free name in a chunk.

Despite the existence of this external _ENV variable and the translation of free names, _ENV is a completely regular name. In particular, you can define new variables and parameters with that name. Each reference to a free name uses the _ENV that is visible at that point in the program, following the usual visibility rules of Lua (see §3.5).

Any table used as the value of _ENV is called an environment.

Lua keeps a distinguished environment called the global environment. This value is kept at a special index in the C registry (see §4.5). In Lua, the global variable _G is initialized with this same value. (_G is never used internally.)

When Lua loads a chunk, the default value for its _ENV upvalue is the global environment (see load). Therefore, by default, free names in Lua code refer to entries in the global environment (and, therefore, they are also called global variables). Moreover, all standard libraries are loaded in the global environment and some functions there operate on that environment. You can use load (or loadfile) to load a chunk with a different environment. (In C, you have to load the chunk and then change the value of its first upvalue.)


lua commands, lua bookmarks

Instruções Assembly Cheat Engine / Lua e Asm