ex5_7.lua 1020 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. local function insert_into(insert, into, pos)
  2. local insert_i = 0
  3. for into_i = pos, #into + #insert do
  4. local new_into_i = into_i + #insert
  5. into[new_into_i] = into[into_i]
  6. into[into_i] = insert[insert_i]
  7. insert_i = insert_i + 1
  8. end
  9. end
  10. local function table_str(t)
  11. local str = "{"
  12. for _, v in pairs(t) do
  13. if type(v) == "string" then
  14. v = '"' .. v .. '"'
  15. end
  16. str = ("%s %s,"):format(str, v)
  17. end
  18. str = str .. " }"
  19. return str
  20. end
  21. local function test(insert, into, pos)
  22. print("------------------------------")
  23. print(("pos: %d"):format(pos))
  24. print(("insert: %s"):format(table_str(insert)))
  25. print(("before: %s"):format(table_str(into)))
  26. insert_into(insert, into, pos)
  27. print(("after: %s"):format(table_str(into)))
  28. print("------------------------------")
  29. end
  30. test({5, 6, 7}, {1, 2, 3, 10, 11}, 4)
  31. test({"a", "b", "c"}, { "a", "b", "c", "d", "e" }, 5)
  32. test({"brave", "new"}, {"hello", "world"}, 2)