Posts by lhf • 1,308 points
33 posts
-
1
votes3
answers151
viewsA: Separate bytes that form an integer number?
Also try to string.byte(string.pack("i4",uint),1,4) Adjust 4 to the size of your numbers.
-
1
votes1
answer220
viewsA: How can I remove all spaces from a string on a moon?
Use gsub. Thus s=s:gsub("%s+",""). The pattern %s+ means "one or more space characters (whitespace)".
-
1
votes1
answer58
viewsA: How do I make a command with substring?
Use string.match as in the example below: s=io.read() c,m=s:match("(%S+)%s+(.+)$") if c=="say" then print(m) end The default used in match is: search and capture a string of characters not spaces,…
-
0
votes1
answer252
viewsA: Concatenate variable names with numbers in Lua
The expression tabela.var1 is equivalent to tabela["var1"] So try print(tabela["var"..x]) However, consider this other way: tabela={} tabela.var={} tabela.var[1]='teste1' tabela.var[2]='teste2'…
-
1
votes1
answer111
viewsA: Create reading of a bool function in lua
Use lua_getfield to take the value of a table field: lua_getglobal(L, "Enableapp"); lua_getfield(L, -1, "Useapp"); useappenabled = lua_toboolean(L,-1); Note the conversion with lua_toboolean and not…
-
3
votes3
answers142
viewsA: Is there any way to get a particular line from a string?
The second line of the string s is the result of s:match("\n(.-)\n").
-
2
votes2
answers280
viewsA: What’s the difference between creating a string with quotes and square brackets?
The main difference is that strings created with quotes accept escape sequences as \n and \t. Strings created with brackets do not interpret any escape sequence. See the manual. The %s in your…
-
9
votes1
answer219
viewsA: How to read a file in Prism?
In Lua The code below reads the full contents of a file: arquivo = 'arquivo.txt' handle = io.open(arquivo, "r") conteudo = handle:read("*a") handle:close() print(conteudo) If you want to handle…
-
1
votes1
answer50
viewsA: Band operation with 64 bits
As the name says, the library bit32 does not support 64 bits, only 32 bits. If you need 64 bits even, you will have to implement your own library using for example strings instead of numbers.…
-
2
votes1
answer166
viewsA: Convert String to Tables
Try the code below: local items = {} local t = "7 2182, 4 2554, 5 9908" local n = 0 for a,b in t:gmatch("(%d+)%s+(%d+)") do n = n + 1 items[n] = {a,b} end for k,v in ipairs(items) do…
-
6
votes2
answers175
viewsA: What is the difference between local`Function var` and local`var = Function...`?
Quoting the manual: The command local function f () corpo end is translated to local f; f = function () corpo end not to local f = function () corpo end (This only makes a difference when the body…
-
3
votes2
answers75
viewsA: How do I remove the thousandths of a value?
Also try to string.format("%.2f",number).
-
2
votes2
answers1178
viewsA: Decrypt a code (as I understand it is in ASCII)
As you seem to know, the string in Code 1 is a pre-compiled version of Code 2. It contains instructions for the Lua virtual machine. You can see a listing by changing Code 1 to io.write(code),…
-
1
votes2
answers212
viewsA: How to escape special characters?
In Lua 5.3, you can use "\u{2191}". The keys are mandatory. Lua 5.3 also includes a library utf8 and you can use utf8.char(2191).
-
4
votes3
answers488
viewsA: What is the 'do' statement’s function?
The main function and usefulness of the do is to create a new scope: local variables defined within that scope do not escape out of it. One reason to use a new scope is not to pollute the rest of…
-
1
votes1
answer854
viewsA: Compile using Terminal in Lua
./a.out x y z receives the strings "x", "y", "z" in argv, beginning in argv[1]. This works exactly the same way in Lua: lua myscript.lua x y z receives the strings "x", "y", "z" in arg, beginning in…
-
1
votes1
answer130
viewsA: Get part of a string
The problem with your code is the use of .*, which is a greedy pattern, that is, take all from that point on. Here is a robust way to pick up all the fields of a line, whatever the order in which…
-
1
votes2
answers232
viewsA: Get LUA IP address
I don’t know if the library socket allows this. An alternative is to read the report of ifconfig with io.popen("ifconfig"):read"*a". If you know the interface name, then you can use that name in the…
-
2
votes3
answers905
viewsA: Browse in LUA tables
If you can create another table, try this: table1={5,10,30,40,50,60,40} table2={10,30,40} values={} for k,v in pairs(table1) do values[v]=true end for k,v in pairs(table2) do if values[v] then…
-
1
votes2
answers340
viewsA: Read only last characters of a LUA string
Try this: s = "12/5/2015;00:00:05;90" print(s:match(".*;(.+)$")) .*; advance to the last ;. (.+)$ captures and returns what is left, until the end of the string.…
-
1
votes1
answer174
viewsA: How to read a moon table in C as follows
Use lua_getglobal(L, "Table") instead of lua_getfield(L, -1, "Table"). Consider using luaL_dofile instead of luaL_loadfile + lua_pcall.…
-
4
votes1
answer125
viewsA: Read LUA file
A way: local l=0 for line in f:lines() do l=l+1 if l>=inicio and l<=fim then table.insert(dat_array, line) end if l>=fim then break end end f:close() Another way: for l=1,inicio-1 do…
-
3
votes1
answer92
viewsA: How to know the last weekend of a month in LUA
Without getting into the merit of daylight saving time, the function below calculates the day of the last Saturday of March of a given year. function last(y) local t={ day=31, month=3, year=y }…
-
4
votes1
answer121
viewsA: Replace text within tag
The problem is that '<.+>' house all the string and the gsub then removes all content. If you want to extract a text, use string.match and catches:…
-
2
votes2
answers467
viewsA: Diff without comparing spaces or line break
Try wdiff, that makes comparison based on words.
-
5
votes1
answer58
viewsA: Two simultaneous routines
Lua does not allow preemptive multitasking, only coroutines, which are collaborative multitasking. Unless the library of socket.http allows simultaneous downloads, the only solution is to run fully…
-
3
votes1
answer882
viewsA: Loadstring, how to use?
If you don’t need to create the global variable st, use b = loadstring("return {b=1, c=2}")() If you want to do this often, consider something like function eval(x) return loadstring("return "..x)()…
-
7
votes1
answer1560
viewsA: In Pairs or Ipairs
ipairs traverse entire index entries in a table, in order. pairs scrolls through all entries in a table, in arbitrary order. In your example, add a.teste=trueafter creating the table or teste=true…
-
3
votes2
answers148
viewsA: String "letter" position
Also try to a = "x x x" for k in a:gmatch("()x") do print(k) end Note the empty capture (), capturing the position.
-
5
votes1
answer82
viewsA: String (g)Match
If you trust this string, then use t = loadstring("return {"..toset.."}")() Then you can check the contents of t with for k,v in pairs(t) do print(k,v) end…
-
3
votes1
answer170
viewsA: How to control screen coordinates?
If you want to move the cursor on a terminal screen, you can use escape sequences for terminals, as in moon-term. Your example would be: term.cursor.goto(10, 10) io.write("um texto") ...…
-
4
votes3
answers644
viewsA: Insert string data into a table
If you already have the file content as string within Lua and trust that content, then you can use Lua to convert everything for you: entries = load("return {"..texto:gsub("\n",",").."}")()…
-
13
votes5
answers9524
viewsA: Receive an expression and calculate in C
If you can use something ready, see libmatheval or my library ae moon-based. With ae, just do v=ae_eval(s), where s is a string containing the expression to be evaluated. This expression may contain…