Forum

> > CS2D > Scripts > What is different (2 Lua Scripts)?
Forums overviewCS2D overview Scripts overviewLog in to reply

English What is different (2 Lua Scripts)?

14 replies
To the start Previous 1 Next To the start

old What is different (2 Lua Scripts)?

Eternal
User Off Offline

Quote
Hello Everyone on us

I have a question about lua , that question is

what different about these code ??

this

Spoiler >


And this

Spoiler >


They aren't same ??

Can someone explain me??

Sorry for bad English

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

old Re: What is different (2 Lua Scripts)?

Alistaire
User Off Offline

Quote
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.

old Re: What is different (2 Lua Scripts)?

Apache uwu
User Off Offline

Quote
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.

old Re: What is different (2 Lua Scripts)?

Alistaire
User Off Offline

Quote
user 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.

old Re: What is different (2 Lua Scripts)?

Apache uwu
User Off Offline

Quote
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.

old Re: What is different (2 Lua Scripts)?

Spook MQ Hacker
BANNED Off Offline

Quote
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.!

old Re: What is different (2 Lua Scripts)?

EngiN33R
Moderator Off Offline

Quote
In short (as user 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.

old Re: What is different (2 Lua Scripts)?

Lee
Moderator Off Offline

Quote
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.

old Re: What is different (2 Lua Scripts)?

EngiN33R
Moderator Off Offline

Quote
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
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview