Getting a key as a number

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Getting a key as a number

Post by stout »

Basic lua question:

Code: Select all

var = { {}, {}, {} }
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?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Getting a key as a number

Post by grump »

stout wrote: Sun Sep 13, 2020 6:36 pm

Code: Select all

var = { {}, {}, {} }
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:

Code: Select all

var = { 1, 2, 3 }
But these would be values, not keys.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Getting a key as a number

Post 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
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Getting a key as a number

Post by stout »

Sorry, I should've added a use case. I want to do:

Code: Select all

if var[2] > 1 then
~other stuff
Right now, that won't work, because var[2] is a table. I want to eval that "2" there, as a number.
TheHUG
Citizen
Posts: 61
Joined: Sun Apr 01, 2018 4:21 pm

Re: Getting a key as a number

Post by TheHUG »

Only way would be to make var[2] a number rather than a table (see grump's answer)
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Getting a key as a number

Post by zorg »

stout wrote: Sun Sep 13, 2020 6:36 pm Basic lua question:

Code: Select all

var = { {}, {}, {} }
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:

Code: Select all

var = { 1, 2, 3 }
Debugging shall continue when you tell us why that's not acceptable. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Getting a key as a number

Post 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
}
User avatar
Nikki
Citizen
Posts: 83
Joined: Wed Jan 25, 2017 5:42 pm

Re: Getting a key as a number

Post 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)
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Getting a key as a number

Post by pgimeno »

stout wrote: Sun Sep 13, 2020 7:32 pm Sorry, I should've added a use case. I want to do:

Code: Select all

if var[2] > 1 then
~other stuff
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:

Code: Select all

if 2 > 1 then
~other stuff
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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 128 guests