Admin/mod comment
"i want to know" = retarded title which tells us nothing about your thread. Try harder next time or your thread will be removed. Title fixed. /DC Basically = Yes
Technically = No 1
2
3
4
5
6
7
A(a, b)
	local c = {}
	for d = 1, a do
		c[d] = b
	end
	return d
end
1
2
3
4
5
6
7
A(a)
	local c = {}
	for d = 1, a do
		c[d] = 0
	end
	return d
end
Difference;
At nr 1 you can choose the value you want the tables to have, at nr 2 you can't. @ MAX-russia: He wants an explanation, not an answer to confirm what he already knows. Read, Max. Yates has written
@
MAX-russia: He wants an explanation, not an answer to confirm what he already knows. Read, Max.
I answered with explanation "Basically" and "Technically". Problems ? Keep on reading..
Yates has written
not an answer to confirm what he already knows.
1
2
3
4
5
6
7
8
9
10
11
function initArray(m)
	return Array(m,0)
end
function Array(size,value)
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end
initArray is just a wrapper for Array. Apache uwu has written
1
2
3
4
5
6
7
8
9
10
11
function initArray(m)
	return Array(m,0)
end
function Array(size,value)
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end
initArray is just a wrapper for Array.
That's the dumbest script I've ever seen. explains it pretty well huh?
1
2
3
4
5
6
7
8
function initArr(size,value)
	value=value or 0
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end
Merged the functions together, so arrays can be set to a value or defaulted to 0. Umm... function initArray(m)
local array = {}
for i = 1, m do
array[i]=0
end
return array
end
function Array(size,value)
local array = {}
for i = 1, size do
array[i]=value
end
return array
end
My total different code.! In short (as Alistaire already said, mind you):
The first function, initArray(m), allows you to create a table, an "array", of size m with all of its entries (from 1 to m) set to zero.
The second function, Array(size,value), lets you create an "array" with the size size with all of its entries (from 1 to size) set to value.
Please do remember that there's no such thing as arrays in Lua, Lua has tables. Lee Moderator Offline
Be careful with the second function as not understanding its limits will usually end up biting you in the ass. What happens when you run the following?
1
2
3
4
5
6
7
8
9
-- I need an array of points, I guess I can just use a table of length two to represent points and have them pre-initialized to be all {0,0}
arr = Array(10, {0,0})
-- Somewhere during the calculation, I needed to add 1 to each of the x components
for i=1,10 do
	arr[i][1] = arr[i][1]+1
	-- I should expect each point to contain (1,0) right?
end
-- For sanity check, is my first point still 1,0?
print(unpack(arr[1]))
The short answer to the above question is What the Fuck? Briefly speaking, don't pass in tables to the Array function. Yes, Lee has a point - if you want to create nested tables, don't do it with the array function. Use a derivation of it or something - like this one:
1
2
3
4
5
6
7
8
9
function doublearray(m,n,k) --m is amount of first-level fields, n is of second-level ones
local a, x, y = {}
for x = 0, m do a[x] = {}
for y = 0, n do
a[x][y] = k
end
end
return a
end
Thank you for all comment , i have known more than before here