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

Table Not In Order?

Started by Neekow, 22 November 2013 - 05:20 PM
Neekow #1
Posted 22 November 2013 - 06:20 PM
Hi everyone!

first of, here's my code (its not big, i'm testing many things):


local IDs = {Paper = {}, White_Ink = {}, Orange_Ink = {}}

function getIDs()
  local event, ID = os.pullEvent("isort_item")
  return ID
end

for i,j in pairs(IDs) do
  IDs[i] = getIDs()
  print(i.." "..IDs[i])
end

The point of this lines is to know IDs (think you understood it). But! it returns:


White_Ink 339
Paper 491871
Orange_Ink 459103

instead of (or, i want this)


Paper 339
White_Ink 491871
Orange_Ink 459103

Why does "White_Ink" come before "Paper"? Think i did a really little mistake but i don't find where ><
theoriginalbit #2
Posted 22 November 2013 - 06:54 PM
no you didn't make a mistake, pairs does not access elements in order unless they are numerically indexed values.


local t = {3,7,6}

for k,v in pairs(t) do
  print(k, ' = ', v)
end
the above code will output
1 = 3
2 = 7
3 = 6

however when dealing with key/value pairs it can be any order (however it is the same order each time)

local t = { key = "value", ["foo"] = "bar", hello = 5, world = 1 }

for k,v in pairs(t) do
  print(k, ' = ', v)
end
could output (since I've not ran the code)
world = 1
foo = bar
key = value
hello = 5

the only thing that we can be certain of is that indexes will be in order even with keys

local t = {1, key = "value", 6, foo = "bar", 8, 9 }

for k,v in pairs(t) do
  print(k, ' = ', v)
end
will output
1 = 1
2 = 6
3 = 8
4 = 9
foo = "bar"
key = "value"

there are always functions that you can make to make sure it comes out in the order you wish, but it requires having some kind of "order"/"rule" element, or wanting it in alphabetical order, or something like that, not something that seems fairly arbitrary.
Neekow #3
Posted 23 November 2013 - 11:56 AM
Yeah, think i found the way to do what i want!

local IDs = {[1] = {"Paper"}, [2] = {"White_Ink"}, [3] = {"Orange_Ink"}}

function getIDs()
  local event, ID = os.pullEvent("isort_item")
  return ID
end

for i,j in pairs(IDs) do
  IDs[i][#IDs[i]+1] = getIDs()
  print(IDs[i][1].." "..IDs[i][2])  
end

This time it returns:



Your help + http://lua-users.org/wiki/TablesTutorial (in fact, your post helped me alot to understand the wiki ^^) = win for me!

thanks ;)/>
jay5476 #4
Posted 23 November 2013 - 04:58 PM
Yeah, think i found the way to do what i want!

local IDs = {[1] = {"Paper"}, [2] = {"White_Ink"}, [3] = {"Orange_Ink"}}

function getIDs()
  local event, ID = os.pullEvent("isort_item")
  return ID
end

for i,j in pairs(IDs) do
  IDs[i][#IDs[i]+1] = getIDs()
  print(IDs[i][1].." "..IDs[i][2])  
end
-snip
you don't need to put the curly braces around your stings, that creates another table you can just have it like this

local IDs = {"Paper","White_Ink","Orange_Ink"}
Neekow #5
Posted 23 November 2013 - 06:13 PM
Huh … but i want "Paper" as a table, to contain ID and Amount … do your thing works in this way? ^^

Have another question … still on tables …
I want to write the table on another file so did:

IDfile = fs.open("ID", "r")
IDfile.write(IDs)
IDfile.close()
Yeah ok, easy, it writes the whole table on the file, but how can i get it back as a table from another program?
Was thinking on IDs = IDfile.read(IDs) but doesn't work and others tests just return me the whole thing as a string … i'm pretty sure it's just a function (something like: totable()) … but don't find anything on that =/
Bomb Bloke #6
Posted 23 November 2013 - 06:17 PM
textutils.unserialize()
Neekow #7
Posted 23 November 2013 - 06:21 PM
… ok was really far far away of this page …
works perfectly, thanks guys =)