Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array with X elements in lua

Tags:

lua

lua-table

Is there any way to declare and reserve a space for a big array without using table.insert? Something like in Python:

a = [0]*10000

or in C:

malloc(10000*sizeof(int))
like image 985
Robert Zaremba Avatar asked Oct 27 '25 06:10

Robert Zaremba


1 Answers

Lua tables are dynamic: they grow as needed. There is no need (and no way) to declare a large array. Just create it with a={} and fill it as needed.

If you must create a large array, just fill it with some value:

a={}
for i=1,10000 do
  a[i]=true
end

In any case, this is not really a job for table.insert.

like image 180
lhf Avatar answered Oct 30 '25 08:10

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!