I have tried to realize adding numbers in lua:
local Calculator = {};
function Calculator.get( frame )
local new_args = str._getParameters( frame.args, { 'f', 'o', 's' } );
local f = tonumber( new_args['f'] ) or 1;
local o = ( new_args['o'] ) or "";
local s = tonumber( new_args['s'] ) or 1;
Calculator.ret(first, second, operation);
end
function Calculator.ret (f, o, s)
if(o == "+") then return f+s;
end
return Calculator
Even if place a end in end, error doesn't disappear.
function Calculator.ret (f, o, s)
if(o == "+") then return f+s end
end ^----------------- here
if in Lua always has to have an end (kinda unlike {} for ifs in C-like languages).
Related
How can i transform this following string:
string = "1,2,3,4"
into a table:
table = {1,2,3,4}
thanks for any help ;)
Let Lua do the hard work:
s="1,2,3,4"
t=load("return {"..s.."}")()
for k,v in ipairs(t) do print(k,v) end
Below is the code adapted from the Scribunto extension to MediaWiki. It allows to split strings on patterns that can be longer than one character.
-- Iterator factory
function string:gsplit (pattern, plain)
local s, l = 1, self:len()
return function ()
if s then
local e, n = self:find (pattern, s, plain)
local ret
if not e then
ret = self:sub (s)
s = nil
elseif n < e then
-- Empty separator!
ret = self:sub (s, e)
if e < l then
s = e + 1
else
s = nil
end
else
ret = e > s and self:sub (s, e - 1) or ''
s = n + 1
end
return ret
end
end, nil, nil
end
-- Split function that returns a table:
function string:split (pattern, plain)
local ret = {}
for m in self:gsplit (pattern, plain) do
ret [#ret + 1] = m
end
return ret
end
-- Test:
local str = '1,2, 3,4'
print ('table {' .. table.concat (str:split '%s*,%s*', '; ') .. '}')
You can use gmatch and the pattern (%d+) to create an iterator and then populate the table.
local input = "1,2,3,4"
local output = {}
for v in input:gmatch("(%d+)") do
table.insert(output, tonumber(v))
end
for _,v in pairs(output) do
print(v)
end
The pattern (%d+) will capture any number of digits(0-9).
This is a narrow solution, it does not handle blank entries such as
input = "1,2,,4"
output = {1,2,4}
It also does not care what the delimitator is, or if it is even consistent for each entry.
input = "1,2 3,4"
output = {1,2,3,4}
Reference:
Lua 5.3 Manual, Section 6.4 – String Manipulation: string.gmatch
FHUG: Understanding Lua Patterns
So I have a string that every 4 characters I need to insert a value like so.
local string = "24029400001000000000000000000000"
--insert : every 4 chars
--output
--2402:9400:0010:0000:0000:0000:0000:0000
The output i am looking for is 2402:9400:0010:0000:0000:0000:0000:0000
function string.chunk( str, n )
local k, t
t= { }
for k in str:gmatch( string.rep( ".", n ) ) do
table.insert( t, k )
end
return t
end
x = "24029400001000000000000000000000"
x_new = ""
for k, v in ipairs( x:chunk( 4 ) ) do
v = v .. ":"
x_new = x_new .. v
end
print(x_new)
--problem is 2402:9400:0010:0000:0000:0000:0000:0000: the : on the end of string
Possible solution:
<script src="https://github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">
local s = "24029400001000000000000000000000"
s = s:gsub('....','%1:'):gsub(':$','')
print(s)
</script>
I have a function defined as a C binding that provides a generic for iterator:
for el in doc:each() do ... end
I want to write an iterator in Lua that iterates using this function, but return a modification of each result. How can I do this?
Edit: I'm sure my iterator has to start something like this, but I'm getting lost in the body of the function.
function myiterator()
local f, c, v = doc:each()
return (function(c2, v2)
-- ??
end), ??, ??
end
function myiterator()
local generator, state, prev_x = doc:each()
local function my_generator()
local x, y = generator(state, prev_x)
if x ~= nil then
prev_x = x
-- modify x, y
local modified_x = x + 100
local modified_y = "("..y..")"
-- modified_x must be non-nil
return modified_x, modified_y
end
end
return my_generator
end
Before:
local doc = {each = function() return ipairs{"aa", "bb", "cc"} end}
for x, y in doc:each() do
print(x, y)
end
Output:
1 aa
2 bb
3 cc
After:
local doc = {each = function() return ipairs{"aa", "bb", "cc"} end}
-- insert myiterator definition here
for x, y in myiterator() do
print(x, y) -- now x and y are modified
end
Output:
101 (aa)
102 (bb)
103 (cc)
I don't know how your C function works, but here is an iterator function that should do what you are looking for. It calls an iterator triplet repeatedly, takes the first return value, and calls a function on it to return a new value:
function map(transformer, f, c, v)
return function()
v = f(c, v)
if v ~= nil then
return transformer(v)
end
end
end
For instance, this takes the keys in the table { 'a', 'b', 'c' } and squares them. The second return value (the values corresponding to the keys) is ignored:
for v in map(function (x) return x * x end, pairs { 'a', 'b', 'c' }) do
print(v)
end
You can do map(function (elem) return do_something_to(elem) end, doc:each()).
It's easier to read the map function when it's written using a coroutine:
function map(transformer, f, c, v)
return coroutine.wrap(function ()
for val in f, c, v do
coroutine.yield(transformer(val))
end
end)
end
For completeness, either of these functions would allow you to use two return values from the original iterator triplet:
function map2(transformer, f, c, v)
return function()
local v2
v, v2 = f(c, v)
if v ~= nil then
return transformer(v, v2)
end
end
end
function map(transformer, f, c, v)
return coroutine.wrap(function ()
for v, v2 in f, c, v do
coroutine.yield(transformer(v, v2))
end
end)
end
for v in map2(function (a, b) return a .. b end, pairs { 'a', 'b', 'c' }) do
print(v)
end
-- This prints out:
-- 1a
-- 2b
-- 3c
You could wrap the iterator into a coroutine, see also https://www.lua.org/pil/9.3.html
-- dummy object
local doc = {
each = function()
return pairs{ 11, 22, 33 }
end
}
local myiterator = coroutine.wrap(function()
local f, c, v = doc:each()
return f, c, v
end)
for f, c, v in myiterator() do
print(f, c, v)
end
$ lua test.lua
1 11 nil
2 22 nil
3 33 nil
Thanks to those that helped. I learned that my iterator function does not HAVE to return 2 or 3 values. This is a valid list-table iterator:
function every(list)
local i=0
return function() -- no arguments used!
i = i+1
return list[i]
end
end
for word in every{'foo', 'bar', 'jim'} do print(word) end
--> foo
--> bar
--> jim
Because every() does not return a second value (the "invariant" value), a nil is passed as the first parameter to the anonymous function on each iteration. The second value passed to that anonymous function is the value returned from the function on a previous pass (or the third return value from every() on the very first pass)...but if that does not help us iterate, we don't need to use it.
Because none of the answers so far exactly answered what I needed, here is the solution I ended up with:
function myiterator()
local f, c, v = doc:each()
return function()
v = f(c,v)
if v then
-- NOTE: Do NOT change the `v` variable here, because
-- f() may expect it to be exactly what it returned
-- in order to iterator properly on the next invocation.
local myvalue = mutate(v)
return myvalue
end
end
end
I need to iterate over some pairs of strings in a program that I am writing. Instead of putting the string pairs in a big table-of-tables, I am putting them all in a single string, because I think the end result is easier to read:
function two_column_data(data)
return data:gmatch('%s*([^%s]+)%s+([^%s]+)%s*\n')
end
for a, b in two_column_data [[
Hello world
Olá hugomg
]] do
print( a .. ", " .. b .. "!")
end
The output is what you would expect:
Hello, world!
Olá, hugomg!
However, as the name indicates, the two_column_data function only works if there are two exactly columns of data. How can I make it so it works on any number of columns?
for x in any_column_data [[
qwe
asd
]] do
print(x)
end
for x,y,z in any_column_data [[
qwe rty uio
asd dfg hjk
]] do
print(x,y,z)
end
I'm OK with using lpeg for this task if its necessary.
function any_column_data(data)
local f = data:gmatch'%S[^\r\n]+'
return
function()
local line = f()
if line then
local row, ctr = line:gsub('%s*(%S+)','%1 ')
return row:match(('(.-) '):rep(ctr))
end
end
end
Here is an lpeg re version
function re_column_data(subj)
local t, i = re.compile([[
record <- {| ({| [ %t]* field ([ %t]+ field)* |} (%nl / !.))* |}
field <- escaped / nonescaped
nonescaped <- { [^ %t"%nl]+ }
escaped <- '"' {~ ([^"] / '""' -> '"')* ~} '"']], { t = '\t' }):match(subj)
return function()
local ret
i, ret = next(t, i)
if i then
return unpack(ret)
end
end
end
It basicly is a redo of the CSV sample and supports quoted fields for some nice use-cases: values with spaces, empty values (""), multi-line values, etc.
for a, b, c in re_column_data([[
Hello world "test
test"
Olá "hug omg"
""]].."\tempty a") do
print( a .. ", " .. b .. "! " .. (c or ''))
end
local function any_column_data( str )
local pos = 0
return function()
local _, to, line = str:find("([^\n]+)\n", pos)
if line then
pos = to
local words = {}
line:gsub("[^%s]+", function( word )
table.insert(words, word)
end)
return table.unpack(words)
end
end
end
Outer loop returns lines, and inner loop returns words in line.
s = [[
qwe rty uio
asd dfg hjk
]]
for s in s:gmatch('(.-)\n') do
for s in s:gmatch('%w+') do
io.write(s,' ')
end
io.write('\n')
end
While reading Programming in Lua, I tried this example given in the book for operator overloading
Set = {}
mt = {}
mt.__add = Set.union
--create a new set with the values of the given list
function Set.new (l)
local set = {}
setmetatable (set, mt)
for _, v in ipairs (l) do
set [v] = true
end
return set
end
function Set.union (a, b)
local result = Set.new {}
for k in pairs (a) do result [k] = true end
for k in pairs (b) do result [k] = true end
return result
end
function Set.intersection (a, b)
local result = Set.new {}
for k in pairs (a) do result [k] = b[k] end
return result
end
function Set.tostring (set)
local l = {}
for e in pairs (set) do
l[#l + 1] = e
end
return "{" .. table.concat (l, ", ") .. "}"
end
function Set.print (s)
print (Set.tostring (s))
end
s1 = Set.new {10, 20, 30, 50}
s2 = Set.new {30, 1}
Set.print (s1)
Set.print (s2)
s3 = s1 + s2
Set.print (s3)
But with the latest lua for windows I am getting the following error
lua: C:\meta.lua:47: attempt to perform arithmetic on global 's1' (a table value)
stack traceback:
C:\meta.lua:47: in main chunk
[C]: ?
{30, 10, 20, 50}
{1, 30}
You are making this assignment too early:
mt.__add = Set.union
because Set.union is not initialized yet.
Move this below Set.union and it will work.
For the same reason, if you assign mt.__mul, this should be below Set.intersection
You need to define mt as a suitable metatable:
mt = { __add = Set.union, __mul = Set.intersection, __tostring = Set.tostring }