lua attempt to call method 'len' (a nil value) - lua

in my lua project I got the following function:
function module.Cp1251ToUtf8(s)
if s == nil then return nil end
local r, b = ''
for i = 1, s and s:len() or 0 do --the problem occurs here
b = s:byte(i)
if b < 128 then
r = r..string.char(b)
else
if b > 239 then
r = r..'\209'..string.char(b - 112)
elseif b > 191 then
r = r..'\208'..string.char(b - 48)
elseif cp1251_decode[b] then
r = r..cp1251_decode[b]
else
r = r..'_'
end
end
end
return r
end
So as far as I understand this function gets a string and converts its encoding. Sometimes it works fine, but sometimes I get the following error: attempt to call method 'len' (a nil value). Any ideas what would it be and how to fix it?
I tried to remove s:len() or insert a condition like if s != nil then ... but it also did not work.

if s == nil then return nil end
The above rejects nil-values. But there are other non-strings, so tighten your check:
if type(s) ~= 'string' return nil end

Related

Currying operations in lua

This question has some reference to the question Defining logical operator implies in lua. The following code works fine.
local function _implies(a, b)
if a == 1 and b == 0 then return 0
else return 1 end
end
local my_right = {
__mul = function(self, b)
return _implies(self.a, b)
end
}
local _mt = {
__mul = function(a)
return setmetatable({a=a}, my_right)
end
}
local my_left={}
setmetatable(my_left,_mt)
imp = my_left
local names={}
for i = 0,1 do
for j=0,1 do
names.i=i;names.j=j
print(i, j, names.i *imp* names.j)
end
end
The following code is similar to above code. It is for logical and. The question I want to ask is : Are both and_left and and_right are required in this case as well? Were they required because implies is not commutative? In other words, can the code be made somewhat efficient for defining logand?
local function _and(a, b)
if a == 1 and b == 1 then return 1
else return 0 end
end
local right_and = {
__mul = function(self, b)
return _and(self.a, b)
end
}
local and_mt = {
__mul = function(a)
return setmetatable({a=a}, right_and)
end
}
local and_left={}
setmetatable(and_left,and_mt)
log_and = and_left
local names={}
for i = 0,1 do
for j=0,1 do
names.i=i;names.j=j
print(i, j, names.i *log_and* names.j)
end
end
Note: I know bitwise operator & is there but for some reasons, I don't want to use it.
Are both and_left and and_right are required
They were introduced because there are two semantically different multiplications involved.
But the code can be optimized.
The version below does not create a new table on every multiplication.
It also uses only one metatable instead of two.
do
local function _implies(a, b)
if a == 1 and b == 0 then
return 0
else
return 1
end
end
local temp, imp = {}, {}
local _mt = {
__mul = function(a, b)
if b == imp then
temp[1] = a
return temp
elseif a == temp then
return _implies(temp[1], b)
end
end
}
setmetatable(temp, _mt)
setmetatable(imp, _mt)
_G.imp = imp
end
local names={}
for i = 0, 1 do
for j = 0, 1 do
names.i=i
names.j=j
print(i, j, names.i *imp* names.j)
end
end

Finding a prime with Miller Rabin

I have what I believe is a proper implementation of the miller-rabin algorithm using Lua, and I am trying to get a consistent return for prime numbers. It seems my implementation only works half of the time. Although if I try implementing similar code within python, that code works 100% of the time. Could someone point me in the right direction?
--decompose n-1 as (2^s)*d
local function decompose(negOne)
exponent, remainder = 0, negOne
while (remainder%2) == 0 do
exponent = exponent+1
remainder = remainder/2
end
assert((2^exponent)*remainder == negOne and ((remainder%2) == 1), "Error setting up s and d value")
return exponent, remainder
end
local function isNotWitness(n, possibleWitness, exponent, remainder)
witness = (possibleWitness^remainder)%n
if (witness == 1) or (witness == n-1) then
return false
end
for _=0, exponent do
witness = (witness^2)%n
if witness == (n-1) then
return false
end
end
return true
end
--using miller-rabin primality testing
--n the integer to be tested, k the accuracy of the test
local function isProbablyPrime(n, accuracy)
if n <= 3 then
return n == 2 or n == 3
end
if (n%2) == 0 then
return false
end
exponent, remainder = decompose(n-1)
--checks if it is composite
for i=0, accuracy do
math.randomseed(os.time())
witness = math.random(2, n - 2)
if isNotWitness(n, witness, exponent, remainder) then
return false
end
end
--probably prime
return true
end
if isProbablyPrime(31, 30) then
print("prime")
else
print("nope")
end
Python has arbitrary length integers. Lua doesn't.
The problem is in witness = (possibleWitness^remainder)%n.
Lua is unable to calculate exact result of 29^15 % 31 directly.
There is a workaround working for numbers n < sqrt(2^53):
witness = mulmod(possibleWitness, remainder, n)
where
local function mulmod(a, e, m)
local result = 1
while e > 0 do
if e % 2 == 1 then
result = result * a % m
e = e - 1
end
e = e / 2
a = a * a % m
end
return result
end

Lua #Table returning 0, despite Table containing 3 elements (tables)? [duplicate]

Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn:
tbl = {}
tbl["test"] = 47
tbl[1] = 48
print(#tbl, table.getn(tbl)) -- prints "1 1"
count = 0
for _ in pairs(tbl) do count = count + 1 end
print(count) -- prints "2"
How do I get the number of all entries without counting them?
You already have the solution in the question -- the only way is to iterate the whole table with pairs(..).
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
Also, notice that the "#" operator's definition is a bit more complicated than that. Let me illustrate that by taking this table:
t = {1,2,3}
t[5] = 1
t[9] = 1
According to the manual, any of 3, 5 and 9 are valid results for #t. The only sane way to use it is with arrays of one contiguous part without nil values.
You can set up a meta-table to track the number of entries, this may be faster than iteration if this information is a needed frequently.
The easiest way that I know of to get the number of entries in a table is with '#'. #tableName gets the number of entries as long as they are numbered:
tbl={
[1]
[2]
[3]
[4]
[5]
}
print(#tbl)--prints the highest number in the table: 5
Sadly, if they are not numbered, it won't work.
There's one way, but it might be disappointing: use an additional variable (or one of the table's field) for storing the count, and increase it every time you make an insertion.
count = 0
tbl = {}
tbl["test"] = 47
count = count + 1
tbl[1] = 48
count = count + 1
print(count) -- prints "2"
There's no other way, the # operator will only work on array-like tables with consecutive keys.
function GetTableLng(tbl)
local getN = 0
for n in pairs(tbl) do
getN = getN + 1
end
return getN
end
You're right. There are no other way to get length of table
You could use penlight library. This has a function size which gives the actual size of the table.
It has implemented many of the function that we may need while programming and missing in Lua.
Here is the sample for using it.
> tablex = require "pl.tablex"
> a = {}
> a[2] = 2
> a[3] = 3
> a['blah'] = 24
> #a
0
> tablex.size(a)
3
local function CountedTable(x)
assert(type(x) == 'table', 'bad parameter #1: must be table')
local new_t = {}
local mt = {}
-- `all` will represent the number of both
local all = 0
for k, v in pairs(x) do
all = all + 1
end
mt.__newindex = function(t, k, v)
if v == nil then
if rawget(x, k) ~= nil then
all = all - 1
end
else
if rawget(x, k) == nil then
all = all + 1
end
end
rawset(x, k, v)
end
mt.__index = function(t, k)
if k == 'totalCount' then return all
else return rawget(x, k) end
end
return setmetatable(new_t, mt)
end
local bar = CountedTable { x = 23, y = 43, z = 334, [true] = true }
assert(bar.totalCount == 4)
assert(bar.x == 23)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = 24
bar.x = 25
assert(bar.x == 25)
assert(bar.totalCount == 4)
I stumbled upon this thread and want to post another option. I'm using Luad generated from a block controller, but it essentially works by checking values in the table, then incrementing which value is being checked by 1. Eventually, the table will run out, and the value at that index will be Nil.
So subtract 1 from the index that returned a nil, and that's the size of the table.
I have a global Variable for TableSize that is set to the result of this count.
function Check_Table_Size()
local Count = 1
local CurrentVal = (CueNames[tonumber(Count)])
local repeating = true
print(Count)
while repeating == true do
if CurrentVal ~= nil then
Count = Count + 1
CurrentVal = CueNames[tonumber(Count)]
else
repeating = false
TableSize = Count - 1
end
end
print(TableSize)
end
seems when the elements of the table is added by insert method, getn will return correctly. Otherwise, we have to count all elements
mytable = {}
element1 = {version = 1.1}
element2 = {version = 1.2}
table.insert(mytable, element1)
table.insert(mytable, element2)
print(table.getn(mytable))
It will print 2 correctly

Error while useing a Method of a Metatable 'attempt to call method (a nil value)'

I get the error 'attempt to call method 'Dot' (a nil value)' while running the following code:
-- Vector2 Class
Vector2 = {X = 0, Y = 0, Magnitude = 0, Unit = nil}
function Vector2.new(XValue,YValue)
local Tmp = {}
setmetatable(Tmp,Vector2)
Tmp.X = XValue
Tmp.Y = YValue
Tmp.Magnitude = math.sqrt(Tmp.X * Tmp.X + Tmp.Y * Tmp.Y)
if Tmp.Magnitude ~= 1 then
Tmp.Unit = Tmp/Tmp.Magnitude
else
Tmp.Unit = Tmp
end
return Tmp
end
-- Arithmetic
function Vector2.__add(A,B)
if getmetatable(A) == getmetatable(B) then
return Vector2.new(A.X+B.X, A.Y+B.Y)
end
end
function Vector2.__sub(A,B)
if getmetatable(A) == getmetatable(B) then
return Vector2.new(A.X-B.X, A.Y-B.Y)
end
end
function Vector2.__mul(A,B)
if tonumber(B) ~= nil then
return Vector2.new(A.X*B, A.Y*B)
end
end
function Vector2.__div(A,B)
if tonumber(B) ~= nil then
return Vector2.new(A.X/B, A.Y/B)
end
end
function Vector2.__unm(Tmp)
return Vector2.new(-Tmp.X, -Tmp.Y)
end
-- Comparisons
function Vector2.__eq(A,B)
if getmetatable(A) == getmetatable(B) then
if A.X == B.X and A.Y == B.Y then
return true
else
return false
end
end
end
function Vector2.__lt(A,B)
if getmetatable(A) == getmetatable(B) then
if A.Magnitude < B.Magnitude then
return true
else
return false
end
end
end
function Vector2.__le(A,B)
if getmetatable(A) == getmetatable(B) then
if A.Magnitude <= B.Magnitude then
return true
else
return false
end
end
end
-- Functionals
function Vector2.__tostring(Tmp)
return Tmp.X..", "..Tmp.Y
end
function Vector2:Dot(B)
return self.X*B.X + self.Y*B.Y
end
function Vector2:Lerp(B,Amn)
return self + (B-self)*Amn
end
print(Vector2.new(1,0.3).Magnitude)
print(Vector2.new(1,0):Dot(Vector2.new(0,1)))
I'm not understanding what I've done wrong, could anyone lend a hand, I have good Lua experience, but have just started learning how to use metatables, so I'm new at this point, I am running it using SciTE, with LuaForWindows. Error is on the very last line, but the line above it runs perfectly
You have forgotten to set __index field:
Vector2 = {X = 0, Y = 0, Magnitude = 0, Unit = nil}
Vector2.__index = Vector2

How to get number of entries in a Lua table?

Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn:
tbl = {}
tbl["test"] = 47
tbl[1] = 48
print(#tbl, table.getn(tbl)) -- prints "1 1"
count = 0
for _ in pairs(tbl) do count = count + 1 end
print(count) -- prints "2"
How do I get the number of all entries without counting them?
You already have the solution in the question -- the only way is to iterate the whole table with pairs(..).
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
Also, notice that the "#" operator's definition is a bit more complicated than that. Let me illustrate that by taking this table:
t = {1,2,3}
t[5] = 1
t[9] = 1
According to the manual, any of 3, 5 and 9 are valid results for #t. The only sane way to use it is with arrays of one contiguous part without nil values.
You can set up a meta-table to track the number of entries, this may be faster than iteration if this information is a needed frequently.
The easiest way that I know of to get the number of entries in a table is with '#'. #tableName gets the number of entries as long as they are numbered:
tbl={
[1]
[2]
[3]
[4]
[5]
}
print(#tbl)--prints the highest number in the table: 5
Sadly, if they are not numbered, it won't work.
There's one way, but it might be disappointing: use an additional variable (or one of the table's field) for storing the count, and increase it every time you make an insertion.
count = 0
tbl = {}
tbl["test"] = 47
count = count + 1
tbl[1] = 48
count = count + 1
print(count) -- prints "2"
There's no other way, the # operator will only work on array-like tables with consecutive keys.
function GetTableLng(tbl)
local getN = 0
for n in pairs(tbl) do
getN = getN + 1
end
return getN
end
You're right. There are no other way to get length of table
You could use penlight library. This has a function size which gives the actual size of the table.
It has implemented many of the function that we may need while programming and missing in Lua.
Here is the sample for using it.
> tablex = require "pl.tablex"
> a = {}
> a[2] = 2
> a[3] = 3
> a['blah'] = 24
> #a
0
> tablex.size(a)
3
local function CountedTable(x)
assert(type(x) == 'table', 'bad parameter #1: must be table')
local new_t = {}
local mt = {}
-- `all` will represent the number of both
local all = 0
for k, v in pairs(x) do
all = all + 1
end
mt.__newindex = function(t, k, v)
if v == nil then
if rawget(x, k) ~= nil then
all = all - 1
end
else
if rawget(x, k) == nil then
all = all + 1
end
end
rawset(x, k, v)
end
mt.__index = function(t, k)
if k == 'totalCount' then return all
else return rawget(x, k) end
end
return setmetatable(new_t, mt)
end
local bar = CountedTable { x = 23, y = 43, z = 334, [true] = true }
assert(bar.totalCount == 4)
assert(bar.x == 23)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = 24
bar.x = 25
assert(bar.x == 25)
assert(bar.totalCount == 4)
I stumbled upon this thread and want to post another option. I'm using Luad generated from a block controller, but it essentially works by checking values in the table, then incrementing which value is being checked by 1. Eventually, the table will run out, and the value at that index will be Nil.
So subtract 1 from the index that returned a nil, and that's the size of the table.
I have a global Variable for TableSize that is set to the result of this count.
function Check_Table_Size()
local Count = 1
local CurrentVal = (CueNames[tonumber(Count)])
local repeating = true
print(Count)
while repeating == true do
if CurrentVal ~= nil then
Count = Count + 1
CurrentVal = CueNames[tonumber(Count)]
else
repeating = false
TableSize = Count - 1
end
end
print(TableSize)
end
seems when the elements of the table is added by insert method, getn will return correctly. Otherwise, we have to count all elements
mytable = {}
element1 = {version = 1.1}
element2 = {version = 1.2}
table.insert(mytable, element1)
table.insert(mytable, element2)
print(table.getn(mytable))
It will print 2 correctly

Resources