我正在编写一个程序来查找数据集的标准偏差。我没有合适的Lua编辑器,因此我正在测试交互式解释器中的所有内容。
在下面的代码中,一切似乎都有效,直到我进入diffsqrd函数。在我调用此函数后,解释器停止让我输入任何内容。我必须关闭它并重新开始。我已经测试了这个函数,没有它之前的代码,它工作正常。
我尝试将整个事件保存为.lua文件并使用dofile运行它,但它做了同样的事情。 我什么都没得到,然后我再也无法输入翻译了。到底是怎么回事?
--a function to see if a file exists
function file_exists(file)
local f=io.open(file, "r")
if f then f:close() end
return f ~= nil
end
--get all lines from a file, returns an empty
--list/table if the file does not exist
function lines_from(file)
if not file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
--Put the .rec file into an array
y_positions=lines_from([[Z:\Octupole stuff\programming\y_p_test.rec]])
--functions to find the standard deviation of an array.
--total, average, difference squared. I stop here because this is the
--minimum code required to cause the problem.
function total(a)
local sum=0
for i,v in ipairs(a) do sum = sum + v
end
return sum
end
function average(a)
if #a==0 then mean=0
else mean=total(a)/#a
end
return mean
end
function diffsqrd(a)
local diff={}
for i in ipairs(a) do
diff[i]=(a[i]-average(a))^2
end
return diff
end
--Use the diffsqrd function on the .rec file.
yd=diffsqrd(y_positions)
print(yd[1])