This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
leftcatcher's profile picture

Question about tables. [Solved]

Started by leftcatcher, 28 October 2012 - 04:18 AM
leftcatcher #1
Posted 28 October 2012 - 05:18 AM
I'm not sure what the problem is, but I'm having issues with using a certain function within a table. It's a menu, and when you select the item, it should open a file and display the contents. The function that does this works just fine. Though I'm trying to give the function call inside the table an argument as well, and that messes it up. When I have it in the first code below, it accesses the page right on the program's startup, instead of just opening the menu and waiting until I select the proper option instead. When I select said option, it gives me the "Can't cocantanate between string and nil" or whatever it is exactly on the showPage function. The menu displays perfectly, though. Below is the code that gives me this problem.


[1] = { dispText = "examplepage", selH = showPage("examplepage1") }

function activateSel( menu ) -- Activates when you hit enter on a selection, in this case, the "examplepage" option.
  menu[select].selH()
end

However, when I do it like this:


[1] = { text = "examplepage", selH = showExampleOnePage }


function showExampleOnePage()
  showPage("examplepage1")
end

Then it works. I can do it this way, sure. But with the several options I would have, it would require me to have many lines of code that I feel I do not need (3 per option, and it would add up quickly), given I can figure out how to make the first code work correctly.

So, if anyone could help me with this, it would be greatly appreciated. Thanks in advance!
Luanub #2
Posted 28 October 2012 - 06:18 AM

function activateSel( menu ) -- Activates when you hit enter on a selection, in this case, the "examplepage" option.
  menu[select].selH()

You've got menu as both an arg for the function activateSel() and as the name of the table menu[select].
ChunLing #3
Posted 28 October 2012 - 06:22 AM
That shouldn't be a problem if menu is a table.

What does the function look like when it is inside the table? If you're using the "function funcname()…end" declaration, that doesn't work inside of tables. You need to use "funcname = function() … end," instead. Don't know if this is your problem since I can't see the whole code.
Luanub #4
Posted 28 October 2012 - 06:31 AM
Is this all you have for the table?


[1] = { text = "examplepage", selH = showExampleOnePage }


I don't use tables like this that often so I may have this wrong I'll verify and update it if so but it should probably look something like

menu = { [1] = { text = "examplepage", selH = "showExampleOnePage" } }
ChunLing #5
Posted 28 October 2012 - 06:36 AM
Yes, that's probably an error unless you gave showExampleOnePage a value somewhere else. Or is that your function?

Oh, wait, it is your function. Okay, try defining it before you try to put it in the table, otherwise the value that's in the table is just nil.

If you want to go back and give a value contained in a table a function definition after the fact, then you can say:
tableidentifier = { text = "examplepage", }
tableidentifier.selH = function()   showPage("examplepage1") end
You don't even need to create the table entry ahead of time.
Edited on 28 October 2012 - 05:40 AM
leftcatcher #6
Posted 28 October 2012 - 06:55 AM
Wait, so, to call the "showPage("examplepage1"), I would have to have it set up as "function () showPage("examplepage1") end" inside the table?

the showExampleOnePage is the method I used to get it to work, but it uses more lines of code and will clutter it all up, so I was trying to make the first code in the OP work, as it will use less.

showPage is an actual function created elsewhere, but I'm trying to give it an argument, then "examplepage1" in the parenthesis. When I do that with how I had it set up, it would somewhat work, it would access the page when I started up the program, when it's supposed to start up by showing a menu, where I press enter and then it shows the page.

EDIT: Solved. That worked.


[1] = { dispText = "examplepage", selH = function() showPage("examplepage1") end }


Also, this format is inside another table. The one luanub mentioned in his latest post. I just took this part to make it simpler, or at least I thought. I forget that I'm used to that method and don't really know other ways all too well, and that some may be the same for the method I am using. :D/>/>
ChunLing #7
Posted 28 October 2012 - 06:59 AM
If you define it inside the table you can't use the "function funcname() … end" style of declaration, you need to use "funcname = function() … end". You don't need to declare it inside the table, you can declare it later, but if you do then you still have to use the "funcname = function() … end" form only funcname has to include the table part because you're outside the table.

You can also define the function before the table, and the table will contain a pointer to the original function (which will also be available by it's original name outside of the table).