Lua table.insert does not accept string parameter

Continuing to study Lua.

I wrote a function that removes the first sentence from each row and returns the result as a table of modified rows where the first sentence was deleted. Oddly enough, it table.insertbehaves strangely in such a function.

function mypackage.remove_first(table_of_lines)
  local lns = table_of_lines
  local new_lns = {}
  for i=1,#lns do
    table.insert(new_lns,string.gsub(lns[i],"^[^.]+. ","",1))
  end
  return new_lns
end

Suddenly this gave me the following error.

[string "function mypackage.remove_first(table_of_lines)..."]:5: bad argument #2 to 'insert' (number expected, got string)

Why is the "number expected" in the first place?

From table.insertdocs

Inserts the value of an element at the pos position in the list, shifting the list of elements [pos], the list [pos + 1], ···, list [#list]. The default value for pos is # list + 1, so there is a call table. insert (t, x) inserts x at the end of the list t.

Nothing is said about type requirements for table.insert. Ok, I decided to change the example.

function mypackage.remove_first(table_of_lines)
  local lns = table_of_lines
  local new_lns = {}
  for i=1,#lns do
    local nofirst = string.gsub(lns[i],"^[^.]+. ","",1)
    table.insert(new_lns,nofirst)
  end
  return new_lns
end

. , ?

+4
1

. :

  • string.gsub ; - .

  • table.insert 3 . 3 , , , , .

  • : func1(func2()), func2 func1, func2 func1, , func1(func2(), something_else) 2 .

, table.insert(ins, string.gsub(...)), 3 , , . .

, :

table.insert(new_lns, (string.gsub(lns[i], "^[^.]+. ", "", 1)))
+2

Source: https://habr.com/ru/post/1666182/


All Articles