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

Function to create multiple tables/arrays

Started by CitizenCrane, 08 August 2014 - 01:26 AM
CitizenCrane #1
Posted 08 August 2014 - 03:26 AM
Hi, I'm having a little trouble with this function I wrote to create tables at specific sizes. I am not particularly good at computercraft, so I am sure I have missed something obvious, but the function will not create tables with the name specified.

Here is the function,



function matrixCreate(x,y,name)
	 name = {} --#creates empty table with the name specified in the third part of the function call
	 for i=1,x do
	 	 name[i] = {} --#this sets all values within the table to tables themselves.
	 	 for j=1,y do
	 	 	 name[i][j] = 1 --#This is just to make sure the table is as big as it needs to be. I would later rewrite these values to different things
	 	 end
	 end
end

matrixCreate(16,16,Grid) --#I also tried "matrixCreate(16,16,"Grid")", but no luck either.

print(Grid[11][2]) --# This was just to test if the table had been successfully created, and the values set.


This function is part of a larger program I am trying to make, that will scan and destroy an area of land, writing the values of the blocks it encounters to an array. Then a second turtle could rebuild the structure elsewhere, using the array as a reference. For 3d structures, the "scanning" turtle would have to be placed down again for each layer, and the program modified to avoid writing over the previous layer's data.

Thanks in advance to anyone who can help me with this,

Will
Edited on 08 August 2014 - 03:41 AM
KingofGamesYami #2
Posted 08 August 2014 - 04:17 AM
Here's the problem: You can't pass "variable names" to a function.

This is obviously wrong:

"hello" = true

Instead, have your function return a value:

local function matrixCreate( x, y ) --#added local, always a good idea unless this is an api.
		 local name = {} --#create empty table
		 for i=1,x do --#fill the table
				 name[i] = {}
				 for j=1,y do
					   name[i][j] = 1
				end
		 end
		 return name --#return the table
end
--#calling the function
local Grid = matrixCreate( 16, 16 ) --#tada
Edited on 08 August 2014 - 02:19 AM
CitizenCrane #3
Posted 08 August 2014 - 05:41 AM
Ah I see. Am I correct in thinking that a variable is created ("Grid"), and it's value is set to what is returned from the function. If it weren't for the "return name" at the end of the function, "Grid" would instead be given the value of the entire function "matrixCreate", essentially duplicating the function?

In any case, thanks for the help :)/>
MKlegoman357 #4
Posted 08 August 2014 - 08:37 AM
If you would emit 'return name' part, the function would return nothing so the 'Grid' variable would be set to nil.
flaghacker #5
Posted 08 August 2014 - 11:32 AM
Also, you don't "dulicate" functions. You would copy the pointer to a certain function into a new variable like so:

local print_backup = print
print_backup("Hello world")
This code copies the pointer to print and uses that new variable to print something.

With parentheses you call a function, without you access the variable that stores the function pointer.
Edited on 08 August 2014 - 09:34 AM
KingofGamesYami #6
Posted 08 August 2014 - 04:56 PM
Ah I see. Am I correct in thinking that a variable is created ("Grid"), and it's value is set to what is returned from the function. If it weren't for the "return name" at the end of the function, "Grid" would instead be given the value of the entire function "matrixCreate", essentially duplicating the function?

In any case, thanks for the help :)/>
Without "return name" Grid would be set to nil. By default, functions return nil.
As flaghacker stated, you can backup functions by using the value, instead of calling them.

local say = print
say( 'Hello' )
Edited on 08 August 2014 - 02:57 PM