Question about indexing tables

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
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Question about indexing tables

Post by MicroMacro »

So, I'm doing something for a school project, and I am setting up an id system using string names, i.e.:

Code: Select all

blocks = {}
blocks["block1"] = {}
I would like to run a for loop to grab the data from all the available blocks. My problem is that a for loop only counts numbers. How do I get it to index the string names with the for loop?

All help is appreciated. Thanks.
https://github.com/ebernerd- where you can find all my work.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Question about indexing tables

Post by bobbyjones »

You can use pairs(t) it runs through every index but doesn't guarantee an order.

Code: Select all

for key, value in pairs(blocks) do
    -- do stuff with the stuff from blocks
end
 
If you need a better example just ask. Also I recommend reading up on your for loops. There are two kinds the generic and numeric for. http://www.lua.org/pil/4.3.html <-- this should help.
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: Question about indexing tables

Post by MicroMacro »

Yeah, I might need a better example. Here's what the full block tables look like:

Code: Select all

blocks = {}
blocks["block1"] = {}
blocks["block1"].name = "block1"
blocks["block1"].funct = function() print("Debug") end

blocks["block2"] = {}
blocks["block2"].name = "block2"
blocks["block2"].funct = function() print("Debug2") end
How would I makea for loop run through both blocks and run both of their functions?
https://github.com/ebernerd- where you can find all my work.
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: Question about indexing tables

Post by MicroMacro »

EDIT: Nevermind. I figured out how to use these again. :ultrahappy:
https://github.com/ebernerd- where you can find all my work.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Question about indexing tables

Post by bobbyjones »

Yay, I was just starting to type. lol
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Question about indexing tables

Post by s-ol »

MicroMacro wrote:Yeah, I might need a better example. Here's what the full block tables look like:

Code: Select all

blocks = {}
blocks["block1"] = {}
blocks["block1"].name = "block1"
blocks["block1"].funct = function() print("Debug") end

blocks["block2"] = {}
blocks["block2"].name = "block2"
blocks["block2"].funct = function() print("Debug2") end
How would I makea for loop run through both blocks and run both of their functions?
By the way, indexing with strings can be done just with the "." , like you also did later. This is exactly the same as your version:

Code: Select all

blocks = {}
blocks.block1= {}
blocks.block1.name = "block1"
blocks.block1.funct = function() print("Debug") end

blocks.block2 = {}
blocks.block2.name = "block2"
blocks.block2.funct = function() print("Debug2") end
Instead, you can also create the same table like this:

Code: Select all

blocks = {
  block1 = {
    name = "block1",
    funct = function() print"Debug" end
  },
  block2 = {
    name = "block2",
    funct  function() print"Debug" end
  }
}

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Question about indexing tables

Post by Ref »

To beat a dead horse, how about simply:

Code: Select all

blocks = {
  {
    name = "block1",
    funct = function() print"Debug" end
  },
  {
    name = "block2",
    funct  function() print"Debug" end
  }
}
and use a simple 'for' loop?

Code: Select all

for I = 1, #blocks do print( blocks[ I ].name ) end
Advert
Prole
Posts: 3
Joined: Fri Mar 09, 2012 3:45 pm

Re: Question about indexing tables

Post by Advert »

Ref wrote:To beat a dead horse, how about simply:

Code: Select all

blocks = {
  {
    name = "block1",
    funct = function() print"Debug" end
  },
  {
    name = "block2",
    funct  function() print"Debug" end
  }
}
and use a simple 'for' loop?

Code: Select all

for I = 1, #blocks do print( blocks[ I ].name ) end
You could use the general ent table, and just keep track of the blocks in a seperate table, as well:

Code: Select all

local blocks = {}
local blockproto = {}

function blockproto:onCreate(id) -- when you create an entity, call entity:onCreate(id)
  blocks[self] = id
end

function blockproto:onDestroy() -- when you destroy an entity, call entity:onDestroy()
  blocks[self] = nil
end

-- you should get the picture from this
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests