Page 1 of 1
Getting a key as a number
Posted: Sun Sep 13, 2020 6:36 pm
by stout
Basic lua question:
I want some way to get var[1] to return as the number 1, var[2] to return as the number 2, etc. Is there something built-in to do this, or would I need a complicated function?
Re: Getting a key as a number
Posted: Sun Sep 13, 2020 7:14 pm
by grump
stout wrote: ↑Sun Sep 13, 2020 6:36 pm
I want some way to get var[1] to return as the number 1, var[2] to return as the number 2, etc. Is there something built-in to do this, or would I need a complicated function?
I'm trying to make sense of your question, but I'm not sure I understand what you mean.
The three tables inside var can be accessed as var[1], var[2], and var[3] as is.
Another interpretation of your question:
But these would be values, not keys.
Re: Getting a key as a number
Posted: Sun Sep 13, 2020 7:21 pm
by pgimeno
I don't understand the question either. Maybe he means something like ipairs()?
Code: Select all
for index, value in ipairs(var) do
-- index is the number, value is the contents at that index
print(index .. ": " .. value)
end
Re: Getting a key as a number
Posted: Sun Sep 13, 2020 7:32 pm
by stout
Sorry, I should've added a use case. I want to do:
Right now, that won't work, because var[2] is a table. I want to eval that "2" there, as a number.
Re: Getting a key as a number
Posted: Sun Sep 13, 2020 7:38 pm
by TheHUG
Only way would be to make var[2] a number rather than a table (see grump's answer)
Re: Getting a key as a number
Posted: Sun Sep 13, 2020 8:30 pm
by zorg
stout wrote: ↑Sun Sep 13, 2020 6:36 pm
Basic lua question:
I want some way to get var[1] to return as the number 1, var[2] to return as the number 2, etc. Is there something built-in to do this, or would I need a complicated function?
Simple, don't do that and instead do this:
Debugging shall continue when you tell us why that's not acceptable.
Re: Getting a key as a number
Posted: Mon Sep 14, 2020 12:58 am
by sphyrth
Let's expand your code:
Code: Select all
var = {
{}, -- var[1] A Table
{}, -- var[2] Another Table
{} -- var[3] And yet another Table
}
For now, keep zorg's suggestion:
Code: Select all
var = {
1, -- var[1] a number
2, -- var[2] and so on
3
}
Re: Getting a key as a number
Posted: Mon Sep 14, 2020 8:15 am
by Nikki
I am not sure I understand completely,
but do you mean something like this:
Code: Select all
local var = {10,11,12}
for k,v in ipairs(var) do
print(k,v)
end
which will print
1 10
2 11
3 12
that 'k' here is the key (1,2,3) and the 'v' is the value (10,11,12)
Re: Getting a key as a number
Posted: Mon Sep 14, 2020 10:40 am
by pgimeno
stout wrote: ↑Sun Sep 13, 2020 7:32 pm
Sorry, I should've added a use case. I want to do:
Right now, that won't work, because var[2] is a table. I want to eval that "2" there, as a number.
The 2 is a value itself. Taken at face value, the answer to your question is:
But I have the hunch that this is not what you want.
Try explaining what are the subtables in the table, and what you're trying to accomplish with them.