This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Joe3000's profile picture

for i,v in pairs() isn't in order?

Started by Joe3000, 16 June 2015 - 02:22 PM
Joe3000 #1
Posted 16 June 2015 - 04:22 PM
This is something strange that I have never dealt with. I have a multidimensional array that all have strings for their indexes/names. However, when I try to go through with for i,v in pairs(), for whatever reason the loop doesn't go through each item consecutively, instead it jumps around to other values. I need to get the number of the index in order to do stuff with the rest of my program.

Because I can't find anything online telling me if there is a way to get the index number of a table by having the index value, I had to do a work around. Instead, I created a basic number at the beginning and every time the loop went through I increased the number, that way I knew what index I was in… but like I said, that doesn't work because I just recently discovered that i,v in pairs doesn't seem to go through the table in order…. which messes EVERYTHING up…. so how do I fix this?


main = {
  cobble = {"cobblestone"}
  stone = {"stone"}
  wood = {"oaklogs"}
}
for i,v in pairs(inventory) do --A different table that I use for other stuff
  tempNumber = 1
  for n,p in pairs(main) do
   if v== l[1] then
    print(tempNumber.. " :  "..l[1])
   end
   tempNumber = tempNumber + 1
  end
end

I really hope what I said makes sense XD it's early in the morning so sorry If you didn't understand anything I was talking about

Edit: For whatever reason the spacing in the code is weird… fixing it right now
Edited on 16 June 2015 - 02:32 PM
Creator #2
Posted 16 June 2015 - 04:29 PM
maybe try
ipairs
Lignum #3
Posted 16 June 2015 - 04:35 PM
In Lua, all tables are maps. Your table definition assigns "cobble" to { "cobblestone" }, "stone" to { "stone" }, etc.. as you can see, your table keys are strings, not numbers which means that there are no "index numbers" as you call them. Because of this, pairs can't figure out the proper order of the entries, simply because there is nothing to order by.

Here's a possible solution to the problem:

local main = {
   {
	  name = "cobble",
	  values = { "cobblestone" }
   },

   {
	  name = "stone",
	  values = { "stone" }
   },

   ...
}

This way you are not manually specifying the table keys. Instead, Lua will assign indexes to each value in the proper order.
Joe3000 #4
Posted 16 June 2015 - 04:42 PM
In Lua, all tables are maps. Your table definition assigns "cobble" to { "cobblestone" }, "stone" to { "stone" }, etc.. as you can see, your table keys are strings, not numbers which means that there are no "index numbers" as you call them. Because of this, pairs can't figure out the proper order of the entries, simply because there is nothing to order by.

Here's a possible solution to the problem:

local main = {
   {
	  name = "cobble",
	  values = { "cobblestone" }
   },

   {
	  name = "stone",
	  values = { "stone" }
   },

   ...
}

This way you are not manually specifying the table keys. Instead, Lua will assign indexes to each value in the proper order.

Thank you :)/> I actually took it a step farther and decided not to name the inner tables at all… just to make it easier… no one else will understand the code but at least I will :D/>
Lignum #5
Posted 16 June 2015 - 04:51 PM
Thank you :)/>/> I actually took it a step farther and decided not to name the inner tables at all… just to make it easier… no one else will understand the code but at least I will :D/>/>

You're welcome!

Most people should easily be able to tell what your code means by the values alone. But if you really feel like making it clearer, that's what comments are for.

Good luck with your program! :)/>