search

Home  >  Q&A  >  body text

Regarding the problem of adding data to table with table.insert

arr={}


for i=1,100 do

-- print(i)

table.insert( arr , 1, i )

end

In the above code, when table.insert in Lua adds data to the table, why does the array structure become {100,99,98,.. ....,3,2,1} instead of {1,2,3,4....99,100}

z_sanz_san2403 days ago1618

reply all(4)I'll reply

  • z_san

    z_san2018-06-15 17:04:01

    Solved. When table.insert adds data to the table, it inserts the first position every time. That is, the table structure for the first cycle is {1}, and the structure for the second cycle is {2,1}. Three times {3,2,1}..., and so on

    reply
    0
  • 小叮当

    小叮当2018-06-14 13:26:21

    Because the data will not be returned until the loop is completed.

    reply
    0
  • 无忌哥哥

    无忌哥哥2018-06-14 10:56:26

    Try replacing --print(i) with ++print(i)

    reply
    0
  • z_san

    --print(i) is a comment

    z_san · 2018-06-15 17:04:56
  • Cancelreply