Jump to content




Table Not In Order?


  • You cannot reply to this topic
6 replies to this topic

#1 Neekow

  • Members
  • 55 posts

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 ><

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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

Quote

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)

Quote

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

Quote

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.

#3 Neekow

  • Members
  • 55 posts

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:

Posted Image

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

thanks ;)

#4 jay5476

  • Members
  • 289 posts

Posted 23 November 2013 - 04:58 PM

View PostNeekow, on 23 November 2013 - 11:56 AM, said:

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"}


#5 Neekow

  • Members
  • 55 posts

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 =/

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 23 November 2013 - 06:17 PM

textutils.unserialize()

#7 Neekow

  • Members
  • 55 posts

Posted 23 November 2013 - 06:21 PM

... ok was really far far away of this page ...
works perfectly, thanks guys =)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users