lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A while back there was some discussion on making arrays that were pre-initialized to a certain size. I think a good way to handle this would be to add one simple function to the language:

table.repeat(size,[value])

This function would return an array of <size> values, with each of the indices from 1 to <size> initialized to <value>, or the index it is at (eg. [1]=1, [2]=2, [3]=3, etc) in the default case.

On top of being useful from a practical implementation standpoint, this would also be useful for situations like table sorting:

function sort_by_secondary_criteria(data,criteria)
  local order=table.repeat(#data)
  table.sort(order,function(m,n) criteria[data[m]] < criteria[data[n]] end)
end

As well as other applications that don't immediately come to mind.