Forum

> > CS2D > Scripts > Array Names
Forums overviewCS2D overview Scripts overviewLog in to reply

English Array Names

11 replies
To the start Previous 1 Next To the start

old Array Names

Blighted Song
User Off Offline

Quote
There is probably a really easy answer to this I just can't find anything about it, so.

All I want to do is make an array for each user however concentraiting doesnt work for me
1
2
3
4
5
i=0
while i<33
	i=i+1
	"array"..i=initArray()
end
returns '=' expected near '..'
same results useing the 'id' variable.

And apparently lua doesnt like strings as variable names. . .

So as I said probably something I overlooked but help is appriciated. Thanks.

old Re: Array Names

Starkkz
Moderator Off Offline

Quote
try this:

1
2
3
4
5
6
7
8
9
function initArray(size,value)
	local a = {}
	for i = 1, size do
		a[i] = value
	end
	return a
end

array = initArray(32,0)

The index is 0, and the arrays doesn't have a length limit.

old Re: Array Names

Starkkz
Moderator Off Offline

Quote
You can get the name / id of the array using tostring function. Try doing this:

1
2
array = {}
print(tostring(array))

That will print you on the console the array name

old Re: Array Names

Blighted Song
User Off Offline

Quote
You don't understand, its not the manipulation that is the problem, I want to have the name of the array as a string.

So the name of the array would be array1 for "array"..id, however lua dosnt like me useing a string as the name for the array.

All I want to know is if there is a way to do just that.

old Re: Array Names

Starkkz
Moderator Off Offline

Quote
Ahh, you mean when you write in the Lua script for example:

1
MyArray = {}

You wanna have that array as a string?. I think it's not possible, but if you have the string and you want to get the array you can get it by the global array.

1
2
MyArray = {}
print(type(_G["MyArray"]))

The global array is _G.

The other thing that can load strings is to convert them. You can use loadstring(the script).

1
2
3
4
5
6
7
8
-- Our string function
myScript = [[
	function (id)
		msg(player(id,"name"))
	end
]]
printName = loadstring(myScript) -- Load the string function
printName(1) -- Execute the function

old Re: Array Names

Blighted Song
User Off Offline

Quote
No, thats not right, I dont want the array as a string.

I want a string as the name of the array, but lua won't exept a string.

So the non working example is "array"..id, which returns array1 (player 1), and I want to use that string ("array1") as the array name.

old Re: Array Names

Starkkz
Moderator Off Offline

Quote
array1 doesn't seem to be array.
array[1] should be right. Theres no way to get it as a string.

old Re: Array Names

archmage
User Off Offline

Quote
I do not know why you would want to do this, but you can do it like so:
More >


It only works with global tables and you use it like:
1
2
3
4
5
6
t = {1, 2, 3, 5, 8}
array_to_string(t)
-- now t1, t2, t3, t4, t5 exist

print(t1, t2, t3, t4, t5)
-- prints: 1, 2, 3, 5, 8

edit:
to create a variable from a string just use
1
loadstring("varname = "..value)()

old Re: Array Names

Lee
Moderator Off Offline

Quote
@Dark Byte:

It's a (marginally) better practice to use _G["name"] = val to set values within the global namespace. Neither is usually recommended unless you're knowingly manipulating the language and understand what it is you're doing.

IE:
1
2
3
4
5
6
function create_n(n, name)
	name = name or "array"
	for i=1,n do
		_G[name..i] = {}
	end
end

The fact that your global namespace is a table also lends to some interesting use cases with metatables.

old Re: Array Names

Starkkz
Moderator Off Offline

Quote
user archmage has written
I do not know why you would want to do this, but you can do it like so:
More >


It only works with global tables and you use it like:
1
2
3
4
5
6
t = {1, 2, 3, 5, 8}
array_to_string(t)
-- now t1, t2, t3, t4, t5 exist

print(t1, t2, t3, t4, t5)
-- prints: 1, 2, 3, 5, 8

edit:
to create a variable from a string just use
1
loadstring("varname = "..value)()


I didn't do that because if you have a similar table it can be confused with other.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview