How to fix "attempt to index ? (a number value)" ? LUA - lua

SOLVED
What I learned: When using a table within a function, make sure you don't have a variable defined under the same string/letter. Local variables overshadow global variables. Hope this helps!
The error occurs on line 112 when trying to use the goto() function.
I'm completely new to coding as of 4 days ago and am attempting to write a code that references a table for an argument to select a parameter for a function.
I want to enter changeD(a[4]) and have checkD() run until it returns a[4]. Instead i get the error code listed in the title.
My table is as follows
a = {}
a[1] = "north"
a[2] = "east"
a[3] = "south"
a[4] = "west"
I reference a table value as an argument in this function:
function changeD(arg)
local dir = 0
repeat
if checkD() == arg then
print("Done")
dir = 1
else
turn(1,1)
end
until dir == 1
end
the checkD() function returns one of the table values. Therefore the changeD() function runs until the value returned by checkD() is the same as the table value specified in the argument.
I apologize if i'm using the wrong words to refer to the table stuff as I am still wet behind the ears when it comes to coding.
My full program is as follows. Sorry if its messy.
c = 0
a = {}
a[1] = "north"
a[2] = "east"
a[3] = "south"
a[4] = "west"
function fd(x)
for i = 1 ,x do
print("forward")
turtle.dig()
turtle.forward()
end
end
function turn(y,x)
if x == 1 then
for i = 1 ,y do
turtle.turnLeft()
print("left")
end
elseif x == 2 then
for i = 1 ,y do
turtle.turnRight()
print("right")
end
end
end
function checkD()
local X, Y, Z = gps.locate()
print(X)
print(Y)
print(Z)
turtle.forward()
local x , y, z = gps.locate()
print(x)
print(y)
print(z)
c = 0
result = false
D = 0
repeat
if c == 0 then
A = X
B = x
print(A.." "..B)
else
A = Z
B = z
print(A.." "..B)
end
sleep(2)
if A < B then
g = 1
elseif A > B then
g = 2
elseif A == B then
c = 1
end
print("compared values; g="..g)
if c == 0 then
if g == 1 then
D = a[2]
result = true
elseif g == 2 then
D = a[4]
result = true
end
print("checked x")
print(D)
elseif not c == 0 then
if g == 1 then
D = a[3]
result = true
elseif g == 2 then
D = a[1]
result = true
end
print("checked z")
end
print("Direction facing "..D)
until result == true
turtle.back()
return D
end
function changeD(arg)
local dir = 0
repeat
if checkD() == arg then
print("Done")
dir = 1
else
turn(1,1)
end
until dir == 1
end
local x = 0
function goto(x,y,z)
local t = 0
local a = 0
local X, Y, Z = gps.locate()
print(X)
print(Y)
print(Z)
if X > x then
t = X - x
changeD(a[4])
fd(t)
elseif X < x then
t = x - X
changeD(a[2])
fd(t)
else
print("cord is same")
end
if Z > z then
t = Z - z
change(a[1])
fd(t)
elseif Z < z then
t = z - Z
change(a[3])
fd(t)
else
print("cord is same")
end
if Y > y then
t = Y - y
for i = 1,t do
turtle.down()
end
elseif Y < y then
t = y - Y
for i = 1,t do
turtle.up()
end
end
end

In goto, you declare local a = 0. This local a variable shadows the global a = {} variable. The local a variable is a number, so can't be indexed.

Related

Computercraft program printing new line instead of updating previous line

I increment y (the y index of the monitor) by one each time I print a line to advance the y value (basically a newline). I'm running into an issue however, when I have done mon_1.setCursorPos(x, y+1) it updates the previous line as it should, however when I do y = y+1 mon_1.setCursorPos(x, y) it prints a new line without updating the previous line. Any suggestions?
Here's the full code
--Define some colors
color_fuel = 16
color_ok = 2243
color_error = 224455
color_blank = 0
color_active = 2240
--Tables
reactors={}
max_energy={}
produced={}
percent_capacity={}
--Globals
x = 1
y = 1
i = 0
--Lamps
lamp_1 = peripheral.wrap("colorful_lamp_0")
lamp_2 = peripheral.wrap("colorful_lamp_1")
lamp_3 = peripheral.wrap("colorful_lamp_2")
--Monitors
mon_1 = peripheral.wrap("monitor_3")
--Cap banks
cap_1 = peripheral.wrap("capacitor_bank_2")
--Helper functions
function flash_fuel_lamp()
lamp_2.setLampColor(color_blank)
sleep(0.01)
lamp_2.setLampColor(color_error)
for i = 10,1,-1 do
lamp_1.setLampColor(color_fuel)
sleep(1)
lamp_1.setLampColor(color_blank)
sleep(1)
end
lamp_1.setLampColor(color_fuel)
end
function clear_lamps()
lamp_1.setLampColor(color_blank)
lamp_2.setLampColor(color_blank)
lamp_3.setLampColor(color_blank)
end
--Boot Sequence
--Set lamps to black and clear screen
clear_lamps()
mon_1.clear()
--Turn on the reactors and set the status light to green
for k,v in pairs(peripheral.getNames()) do
if(string.find(v, "BigReactors")) then
reactors[i] = peripheral.wrap(v)
i = i + 1
else
--print("Other Peripheral Detected")
end
end
for k,v in pairs(reactors) do
v.setActive(true)
end
lamp_2.setLampColor(color_ok)
lamp_3.setLampColor(color_active)
--Main loop
for key,value in pairs(max_energy) do
mon_1.setCursorPos(x, y)
mon_1.write("Reactor " .. key .. " Max Energy: " .. value)
y = y + 1
end
while true do
--Check the fuel level
if(redstone.getAnalogInput("bottom") == 0) then
lamp_3.setLampColor(color_blank)
for k,v in pairs(reactors) do
v.setActive(false)
end
flash_fuel_lamp()
end
for k,v in pairs(reactors) do
if(v.getActive() == false) then
lamp_3.setLampColor(color_blank)
end
--Calculate the energies
max_energy[k] = v.getEnergyStored()
produced[k] = v.getEnergyProducedLastTick()
percent_capacity[k] = (max_energy[k] / produced[k]) * 100
end
--[[max_energy = reactor.getEnergyStored()
energy_produced = reactor.getEnergyProducedLastTick()
percent_capacity = (max_energy / energy_produced) * 100]]
max_energy_cap = cap_1.getMaxEnergyStored()
energy_stored = cap_1.getEnergyStored()
percent_capacity_cap = (energy_stored / max_energy_cap) * 100
y=y+1
mon_1.setCursorPos(x, y)
for key,value in pairs(percent_capacity) do
for k,v in pairs(reactors) do
mon_1.write("Reactor " .. k .. " Percent Capacity: " .. value)
y=y+1
mon_1.setCursorPos(x, y)
mon_1.write("Capacitor output per tick: " .. cap_1.getAverageOutputPerTick())
y=y+1
mon_1.setCursorPos(x, y)
mon_1.write("Capacitor input per tick: " .. cap_1.getAverageInputPerTick())
y=y+1
mon_1.setCursorPos(x, y)
end
end
sleep(0.01)
end```
So I think that you might be forgetting that when you do mon_1.setCursorPos(x, y+1) you're not incrementing the y variable, you're only passing a higher value to the called function, while with y = y+1 mon_1.setCursorPos(x, y) you are incrementing y.
This explains why the previous line is not being updated when incrementing y, since the nested for-loops at the bottom of your code use that y variable as well.

math library is missing in the latest update of Logitech G-Hub

local delay = math.random(25, 50)
[string "LuaVM"]:5: attempt to index a nil value (global 'math')
I can't use math.random anymore is there any way to fix this ?
If math library is missed you can insert the following code block at the beginning of your script.
It will not fix the whole math library, but only some of the most frequently used functions (including math.random).
It will also fix the following errors:
bad argument #1 to 'Sleep' (number has no integer representation)
attempt to call a nil value (field 'getn')
do
local state_8, state_45, cached_bits, cached_bits_qty = 2, 0, 0, 0
local prev_width, prev_bits_in_factor, prev_k = 0
for c in GetDate():gmatch"." do
state_45 = state_45 % 65537 * 23456 + c:byte()
end
local function get_53_random_bits()
local value53 = 0
for shift = 26, 27 do
local p = 2^shift
state_45 = (state_45 * 233 + 7161722017421) % 35184372088832
repeat state_8 = state_8 * 76 % 257 until state_8 ~= 1
local r = state_8 % 32
local n = state_45 / 2^(13 - (state_8 - r) / 32)
n = (n - n%1) % 2^32 / 2^r
value53 = value53 * p + ((n%1 * 2^32) + (n - n%1)) % p
end
return value53
end
for j = 1, 10 do get_53_random_bits() end
local function get_random_bits(number_of_bits)
local pwr_number_of_bits = 2^number_of_bits
local result
if number_of_bits <= cached_bits_qty then
result = cached_bits % pwr_number_of_bits
cached_bits = (cached_bits - result) / pwr_number_of_bits
else
local new_bits = get_53_random_bits()
result = new_bits % pwr_number_of_bits
cached_bits = (new_bits - result) / pwr_number_of_bits * 2^cached_bits_qty + cached_bits
cached_bits_qty = 53 + cached_bits_qty
end
cached_bits_qty = cached_bits_qty - number_of_bits
return result
end
table = table or {}
table.getn = table.getn or function(x) return #x end
math = math or {}
math.huge = math.huge or 1/0
math.abs = math.abs or function(x) return x < 0 and -x or x end
math.floor = math.floor or function(x) return x - x%1 end
math.ceil = math.ceil or function(x) return x + (-x)%1 end
math.min = math.min or function(x, y) return x < y and x or y end
math.max = math.max or function(x, y) return x > y and x or y end
math.sqrt = math.sqrt or function(x) return x^0.5 end
math.pow = math.pow or function(x, y) return x^y end
math.frexp = math.frexp or
function(x)
local e = 0
if x == 0 then
return x, e
end
local sign = x < 0 and -1 or 1
x = x * sign
while x >= 1 do
x = x / 2
e = e + 1
end
while x < 0.5 do
x = x * 2
e = e - 1
end
return x * sign, e
end
math.exp = math.exp or
function(x)
local e, t, k, p = 0, 1, 1
repeat e, t, k, p = e + t, t * x / k, k + 1, e
until e == p
return e
end
math.log = math.log or
function(x)
assert(x > 0)
local a, b, c, d, e, f = x < 1 and x or 1/x, 0, 0, 1, 1
repeat
repeat
c, d, e, f = c + d, b * d / e, e + 1, c
until c == f
b, c, d, e, f = b + 1 - a * c, 0, 1, 1, b
until b <= f
return a == x and -f or f
end
math.log10 = math.log10 or
function(x)
return math.log(x) / 2.3025850929940459
end
math.random = math.random or
function(m, n)
if m then
if not n then
m, n = 1, m
end
local k = n - m + 1
if k < 1 or k > 2^53 then
error("Invalid arguments for function 'random()'", 2)
end
local width, bits_in_factor, modk
if k == prev_k then
width, bits_in_factor = prev_width, prev_bits_in_factor
else
local pwr_prev_width = 2^prev_width
if k > pwr_prev_width / 2 and k <= pwr_prev_width then
width = prev_width
else
width = 53
local width_low = -1
repeat
local w = (width_low + width) / 2
w = w - w%1
if k <= 2^w then
width = w
else
width_low = w
end
until width - width_low == 1
prev_width = width
end
bits_in_factor = 0
local bits_in_factor_high = width + 1
while bits_in_factor_high - bits_in_factor > 1 do
local bits_in_new_factor = (bits_in_factor + bits_in_factor_high) / 2
bits_in_new_factor = bits_in_new_factor - bits_in_new_factor%1
if k % 2^bits_in_new_factor == 0 then
bits_in_factor = bits_in_new_factor
else
bits_in_factor_high = bits_in_new_factor
end
end
prev_k, prev_bits_in_factor = k, bits_in_factor
end
local factor, saved_bits, saved_bits_qty, pwr_saved_bits_qty = 2^bits_in_factor, 0, 0, 2^0
k = k / factor
width = width - bits_in_factor
local pwr_width = 2^width
local gap = pwr_width - k
repeat
modk = get_random_bits(width - saved_bits_qty) * pwr_saved_bits_qty + saved_bits
local modk_in_range = modk < k
if not modk_in_range then
local interval = gap
saved_bits = modk - k
saved_bits_qty = width - 1
pwr_saved_bits_qty = pwr_width / 2
repeat
saved_bits_qty = saved_bits_qty - 1
pwr_saved_bits_qty = pwr_saved_bits_qty / 2
if pwr_saved_bits_qty <= interval then
if saved_bits < pwr_saved_bits_qty then
interval = nil
else
interval = interval - pwr_saved_bits_qty
saved_bits = saved_bits - pwr_saved_bits_qty
end
end
until not interval
end
until modk_in_range
return m + modk * factor + get_random_bits(bits_in_factor)
else
return get_53_random_bits() / 2^53
end
end
local orig_Sleep = Sleep
function Sleep(x)
return orig_Sleep(x - x%1)
end
end

how do you make a string dictionary function in lua?

Is there a way if a string is close to a string in a table it will replace it with the one in the table?
Like a spellcheck function, that searches through a table and if the input is close to one in the table it will fix it , so the one in the table and the string is the same?
You can use this code :) Reference code is from here : https://github.com/badarsh2/Algorithm-Implementations/blob/master/Levenshtein_distance/Lua/Yonaba/levenshtein.lua
local function min(a, b, c)
return math.min(math.min(a, b), c)
end
local function matrix(row,col)
local m = {}
for i = 1,row do m[i] = {}
for j = 1,col do m[i][j] = 0 end
end
return m
end
local function lev(strA,strB)
local M = matrix(#strA+1,#strB+1)
local i, j, cost
local row, col = #M, #M[1]
for i = 1, row do M[i][1] = i - 1 end
for j = 1, col do M[1][j] = j - 1 end
for i = 2, row do
for j = 2, col do
if (strA:sub(i - 1, i - 1) == strB:sub(j - 1, j - 1)) then cost = 0
else cost = 1
end
M[i][j] = min(M[i-1][j] + 1,M[i][j - 1] + 1,M[i - 1][j - 1] + cost)
end
end
return M[row][col]
end
local refTable = {"hell", "screen"}
local function getClosestWord(pInput, pTable, threesold)
cDist = -1
cWord = ""
for key, val in pairs(pTable) do
local levRes = lev(pInput, val)
if levRes < cDist or cDist == -1 then
cDist = levRes
cWord = val
end
end
print(cDist)
if cDist <= threesold then
return cWord
else
return pInput
end
end
a = getClosestWord("hello", refTable, 3)
b = getClosestWord("screw", refTable, 3)
print(a, b)
Third parameter is threesold, if min distance is higher than threesold, word is not replaced.

Is there a way to add a numerical value to a variable in lua in a for loop?

I made this program:
a = math.random(0, 9) - 0 -- In this stage, if the result equals zero, that means it's a match
b = math.random(0, 9) - 1
c = math.random(0, 9) - 2
d = math.random(0, 9) - 3
e = math.random(0, 9) - 4
f = math.random(0, 9) - 5
g = math.random(0, 9) - 6
h = math.random(0, 9) - 7
i = math.random(0, 9) - 8
j = math.random(0, 9) - 9
print("Enter the number of trials you want to simulate.") -- This is where I decide how many trials I want to do
var = io.read()
a_ = 0 -- This is where I hope to keep the number of "a" matches, number of "b" matches, etc. The frequency
b_ = 0
c_ = 0
d_ = 0
e_ = 0
f_ = 0
g_ = 0
h_ = 0
i_ = 0
j_ = 0
for k = 1, var do -- One loop is a trial
print("Trail #"..k)
if a == 0 then
print("a = match")
elseif a ~= 0 then
print("a = not a match")
end
if b == 0 then
print("b = match")
elseif b ~= 0 then
print("b = not a match")
end
if c == 0 then
print("c = match")
elseif c ~= 0 then
print("c = not a match")
end
if d == 0 then
print("d = match")
elseif d ~= 0 then
print("d = not a match")
end
if e == 0 then
print("e = match")
elseif e ~= 0 then
print("e = not a match")
end
if f == 0 then
print("f = match")
elseif f ~= 0 then
print("f = not a match")
end
if g == 0 then
print("g = match")
elseif g ~= 0 then
print("g = not a match")
end
if h == 0 then
print("h = match")
elseif h ~= 0 then
print("h = not a match")
end
if i == 0 then
print("i = match")
elseif i ~= 0 then
print("i = not a match")
end
if j == 0 then
print("j = match")
elseif j ~= 0 then
print("j = not a match")
end
end
while true do --This is just to keep the window open after the program is done so that I can observe the data, you can ignore this
end
As you can see, I tried to add one to a_, b_ and c_ every time it returns a result of zero, but it doesn't work, it there a way to do this?
The reason I want to do this is for an AP stats class I'm taking, and this will make it a lot easier to do. I'm just doing a_, b_, c_ for now, once I solve this issue, I'll do all of them. Thanks for reading!
Assuming you want the simulation to run 'var' times, try this:
math.randomseed(os.time())
local matchStorage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local randomNums = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local function runSimulation(numTrial)
print("\nRunning trial #: " .. numTrial)
for index, value in pairs(randomNums) do
local variable = math.random(0, 9)
randomNums[index] = variable
end
for index, value in pairs(randomNums) do
if randomNums[index] == 0 then
matchStorage[index] = matchStorage[index] + 1
print("index " .. index .. " is a match.")
else
print("index " .. index .. " is not a match.")
end
end
end
do
print("Enter the number of trials")
numTrials = io.read()
for index = 1, numTrials do
runSimulation(index)
end
print("\nRESULTS:\n")
for index, value in pairs(matchStorage) do
print("index " .. index .. " frequency: " .. value)
end
end
The number of times that one of the following 'randomNum' values contain a 0 will be stored in its corresponding 'matchStorage' index.

Table value doesn't change

I have a 2 dim array and all it's cells filled with zeros.
What i'm trying to do is to take some randomly chosen cells and fill it with 4 or 5
but what i get is either empty gird with all value equal to zero or i get just one value that has changed to 4 or 5 and that's my code below:
local grid = {}
for i=1,10 do
grid[i] = {}
for j=1,10 do
grid[i][j] = 0
end
end
local empty={}
for i=1,10 do
for j=1,10 do
if grid[i][j]==0 then
table.insert(empty,i ..'-'.. j)
end
end
end
local fp=math.floor(table.maxn(empty)/3)
local fx,fy
for i=1,fp do
math.randomseed(os.time())
math.random(0,1)
local fo=math.random(0,1)
math.random(table.maxn(empty))
local temp= empty[math.random(table.maxn(empty))]
local dashindex=string.find(temp,'-')
fx=tonumber(string.sub(temp,1,dashindex-1))
fy=tonumber(string.sub(temp,dashindex+1,string.len(temp)))
if fo==0 then
grid[fx][fy]=4
elseif fo==1 then
grid[fx][fy]=5
end
end
for i=1,10 do
for j=1,10 do
print(grid[i][j])
end
print('\n')
end
I'm not sure what the for i=1,fp loop is doing with temp and fo, for example the seed should only be set once, and also, the return value on line after local fo is ignored, seems very messy. But based on your post, if you really just want to randomly select N cells from your 2D array and set those to either 4 or 5 (randomly), this should work:
-- maybe N = fp
local N = 5
math.randomseed(os.time())
local i = 1
repeat
fx = math.random(1, 10)
fy = math.random(1, 10)
if grid[fx][fy] == 0 then
grid[fx][fy] = math.random(4,5)
i = i + 1
end
until i > N
Note however that the closer N is to number of items in array (100 in your example), the longer it will take for the loop to complete. If this is a concern, then for large N values, you could do the opposite: initialize each cell to 4 or 5 randomly, and then randomly set size - N of them to 0.
math.randomseed(os.time())
local rows = 10
local columns = 10
local grid = {}
if N > rows*columns/2 then
for i=1,rows do
grid[i] = {}
for j=1,columns do
grid[i][j] = math.random(4,5)
end
end
local i = 1
repeat
fx = math.random(1, 10)
fy = math.random(1, 10)
if grid[fx][fy] ~= 0 then
grid[fx][fy] = 0
i = i + 1
end
until i > N
else
for i=1,rows do
grid[i] = {}
for j=1,columns do
grid[i][j] = 0
end
end
local i = 1
repeat
fx = math.random(1, 10)
fy = math.random(1, 10)
if grid[fx][fy] == 0 then
grid[fx][fy] = math.random(4,5)
i = i + 1
end
until i > N
end

Resources