Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lua, how should I handle a zero-based array index which comes from C?

Tags:

c

lua

lua-table

Within C code, I have an array and a zero-based index used to lookup within it, for example:

char * names[] = {"Apple", "Banana", "Carrot"};
char * name = names[index];

From an embedded Lua script, I have access to index via a getIndex() function and would like to replicate the array lookup. Is there an agreed on "best" method for doing this, given Lua's one-based arrays?

For example, I could create a Lua array with the same contents as my C array, but this would require adding 1 when indexing:

names = {"Apple", "Banana", "Carrot"}
name = names[getIndex() + 1]

Or, I could avoid the need to add 1 by using a more complex table, but this would break things like #names:

names = {[0] = "Apple", "Banana", "Carrot"}
name = names[getIndex()]

What approach is recommended?

Edit: Thank you for the answers so far. Unfortunately the solution of adding 1 to the index within the getIndex function is not always applicable. This is because in some cases indices are "well-known" - that is, it may be documented that an index of 0 means "Apple" and so on. In that situation, should one or the other of the above solutions be preferred, or is there a better alternative?

Edit 2: Thanks again for the answers and comments, they have really helped me think about this issue. I have realized that there may be two different scenarios in which the problem occurs, and the ideal solution may be different for each.

In the first case consider, for example, an array which may differ from time to time and an index which is simply relative to the current array. Indices have no meaning outside the code. Doug Currie and RBerteig are absolutely correct: the array should be 1-based and getIndex should contain a +1. As was mentioned, this allows the code on both the C and Lua sides to be idiomatic.

The second case involves indices which have meaning, and probably an array which is always the same. An extreme example would be where names contains "Zero", "One", "Two". In this case, the expected value for each index is well-known, and I feel that making the index on the Lua side one-based is unintuitive. I believe one of the other approaches should be preferred.

like image 639
user200783 Avatar asked Apr 23 '13 08:04

user200783


People also ask

Are Lua arrays zero indexed?

Yes, the arrays in Lua start with index 1 as the first index and not index 0 as you might have seen in most of the programming languages.

Are arrays 0 indexed in C?

Master C and Embedded C Programming- Learn as you go So *(arr+i) means the element at i distance from the first element of the array. So array index starts from 0 as initially i is 0 which means the first element of the array.

Does Lua count from 0 or 1?

Lua libraries prefer to use indices which start at 1. However, you can use any index you want. You can use 0, you can use 1, you can use -5.

Does Lua indexing start at 1?

In Lua, indexing generally starts at index 1. But it is possible to create objects at index 0 and below 0 as well. Array using negative indices is shown below where we initialize the array using a for loop.


1 Answers

Use 1-based Lua tables, and bury the + 1 inside the getIndex function.

like image 184
Doug Currie Avatar answered Sep 21 '22 12:09

Doug Currie