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

Trying to set values in a table with keys other than 1 seems to not work as I expect

Started by NiteLite, 07 July 2013 - 11:44 AM
NiteLite #1
Posted 07 July 2013 - 01:44 PM
I am sure this is because of some small error I am doing / something I have misunderstood. I came across this when making a program that monitors my wireless turtles. I am sending out a message on the rednet and trying storing the status for each turtle that reports back in a table with their computer ID as the key. Looking at http://www.lua.org/pil/2.5.html I was assuming that I should be able to set numeric keys other then "the first one" / 1, but this doesnt seem to work as I expected.

I have been trying things out in a small test program I made to narrow down what I was struggling with:


t = {}

function setit(cid)
  t[cid] = "somevalue"..cid
end

print("13 Table size was "..table.getn(t))
setit(13)
print("13 Table size is now "..table.getn(t))
textutils.tabulate(t)

print("1  Table size was "..table.getn(t))
setit(1)
print("1  Table size is now "..table.getn(t))

print("15 Table size was "..table.getn(t))
setit(15)
print("15 Table size is now "..table.getn(t))
textutils.tabulate(t)

This gives me the following output:

13 Table size was 0
13 Table size is now 0
1  Table size was 0
1  Table size is now 1
15 Table size was 1
15 Table size is now 1
somevalue1

I was expecting this to set all three keys actually, based on the examples I have found etc. Could one of the pros (or maybe even noobs?) give me a hint as to what I have misinterpreted about hos Tables work in Lua ?
Lyqyd #2
Posted 07 July 2013 - 02:25 PM
Split into new topic.

Your tables are working fine. table.getn(t) returns either the value n set by table.setn(t, n), or, failing that, returns the last non-nil index it finds, starting at one, or if one is nil, returns zero. You will want to use table.setn if you actually need to know the number of non-nil entries in the table, and the best way to know with certainty that it is accurate is to use it in a function as part of the __newindex metamethod for that table. If you don't actually need to know the number of non-nil entries, you won't need to worry about that, though. Just use pairs() to iterate the table in that case, no further work will be necessary.
NiteLite #3
Posted 07 July 2013 - 05:36 PM
A-ha!. Seems like I had misunderstood what table.getn(table) actually returned. I was expecting it to behave like count() in PHP (which returns the number of elements in the list). Thanks for the tip! I will continue my quest to get the tables working with this information at hand :)/>
AgentE382 #4
Posted 07 July 2013 - 07:52 PM
A-ha!. Seems like I had misunderstood what table.getn(table) actually returned. I was expecting it to behave like count() in PHP (which returns the number of elements in the list). Thanks for the tip! I will continue my quest to get the tables working with this information at hand :)/>/>

It does return the number of elements in the list. Provided, of course, that the table is a sequence. Your table isn't a sequence, so neither `getn()` nor the `#` operator will work the way you think.

Read 2.5.5 of the Lua 5.1 manual and 3.4.6 of the Lua 5.2 manual to get a good understanding of how it works.

Simple illustration: http://codepad.org/ktHj9FbO
theoriginalbit #5
Posted 08 July 2013 - 01:39 AM
As stated above table.getn, table.maxn, and the `#` operator all operate the same and require the values to be index values and in sequence. As Lyqyd stated use a pairs loop to count all the elements in a table (indexed, non-sequential indexed, and key/value pairs). Example:


local function count( t )
  local count = 0
  for _ in pairs( t ) do
    count = count + 1
  end
  return count
end
using the above code on your table will return the size of the table, so usage:

local example = {
 "this",
  2,
  [4] = 5,
  key = "value",
  ['this'] = "that"
}

print( #example ) --# will print 2
print( table.getn(example) ) --# will print 2
print( table.maxn(example) ) --# will print 2
print( count(example) )--# will print 5