1

I have a piece of Lua code that generate an error and I don't understand how to solve it.

  .............................
 local last_num = 0 
 local  channelTable={} 
 for num in channels.each_number() do  --  channels.each_number() returns 1.number  in each call 
  channelTable[last_num] =num;
  last_num = last_num +1;
 end    
 table.sort(channelTable);

based on lua documentation I can use the function sort to sort the saved numbers in channelTable. the error that I get is:

attempt to index global 'table'

Any idea how can I solve this, or should implement bubble sort? thanks for any hint!

1
  • Can you run a simple script separately that prints out the _G table? That'll at least tell us if table and other standard lua facilities are available in your environment. Commented Jan 5, 2015 at 22:34

3 Answers 3

2

Either you haven't loaded the table library or you have overwritten it by accident.

The error message seems truncated: it should say why indexing failed.

Sign up to request clarification or add additional context in comments.

4 Comments

that the error message that I get, I'm using Lua on a HTML page run on ARM9. and that all I get the rest of the error message is only about the path of the lua file, how can load the table ? by the way thanks for your answer !
@Engine, there should be (a xxx value) at the end of the message.
thant a lot for you help but No : I'm using haserl CGI !
thanks again for your the lib was explecitly rejected, I didn't know since I didn't stared the project !
1

The error you are seeing indicates that the table library is not available. It's unlikely that this core library isn't part of your Lua environment, so it's likely you have assigned something to table elsewhere in your code.

2 Comments

thanks for your answer, is there away to load it explicitly using the require statement
As I understand it, the base libraries are built into the Lua interpreter at compile time, and can't be loaded as modules, as there is no library to look in to find them. I could be completely wrong though - I've never used a Lua environment where the table library wasn't available. What do you get if you try: print(table); to see what the name refers to?
0

I think the issue may be that you are expecting channels.each_number() to be called in each iteration of the loop. If I'm not mistaken, I think it only gets called the first time the program goes through the loop. Whatever you use in thefor..in loop needs to be a table, I believe. So I guess the problem is that your table isn't being generated as you want it to. Try doing this:

print('number of items in channelTable = ' .. #channelTable)

If it comes out to 0, then what I said is probably the problem.

1 Comment

No defintly not the code works exactly the way I want when out comment the table line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.