如何在变量中存储特定的文本文件行

时间:2015-11-15 19:32:57

标签: string lua

我正在尝试将文本文件中的特定行存储到变量中。

file = io.open("words.lua", "r")
randLine = math.random(1, 109583)
local n = 0
for l in io.lines("words.lua") do
  n = n + 1
  if n == randLine then
    word = randLine
  end
end

我想将字符串存储在randLine的{​​{1}}上。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

lines是一个返回行的迭代器,而不是行号。

file = io.open("words.lua", "r")
randLine = math.random(1, 109583)
local n = 0
for line in file:lines() do
  n = n + 1
  if n == randLine then
    word = line
  end
end
file:close()
相关问题