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

a question concerning tables

Started by thedriver87, 22 June 2013 - 01:31 PM
thedriver87 #1
Posted 22 June 2013 - 03:31 PM
i am attempting to write a string to a table then call it back like so.
for i=1,x do
ans=read()
anstable={ =ans}
end

and then

for i=1,x do
ans=anstable
print("your sting is",ans,"!")
end

how ever it doesnt seem to print out all the strings. seems like only the last one prints properly what am i missing here?
Lyqyd #2
Posted 22 June 2013 - 03:39 PM
You're redeclaring the table each time, so only the last string will be in the table.
thedriver87 #3
Posted 22 June 2013 - 03:54 PM
so that is why if there are four then 1= 2= 3= are written over by 4=? then how do i go about declaring a number of variables based on an input?
as i was attempting to write X number of variables into the table so that depending on the number selected by the user that many may be stored.
ElvishJerricco #4
Posted 22 June 2013 - 03:56 PM
table.insert just adds to the end of the table (or inserts anywhere if you give it an index but I won't for this). Also, it's good to use locals for all your variables because they're faster and any globals declared can be accessed by another program run in the same shell.


local anstable = {}
for i=1,x do
    table.insert(anstable, read())
end
Also, iterative for loops are cool.

for i,v in ipairs(anstable) do
    print("Answer #" .. i .. ": " .. v)
end
thedriver87 #5
Posted 22 June 2013 - 04:58 PM
thank you for the advice i am self taught and things like this are always ureka moments for me
thedriver87 #6
Posted 22 June 2013 - 05:00 PM
i am a little confused by the for i,v in ipairs(anstable) what is the purpose of this? as oppose to starting a count at 1 to the variable like i did?
Kingdaro #7
Posted 22 June 2013 - 05:02 PM

local anstable = {}
for i=1,x do
    table.insert(anstable, read())
end

This wouldn't do anything, as x is undefined, though you were probably just making an example. If you were to define x, however, this would be a much faster, and more straight-forward solution:


local anstable = {}
for i=1, 10 do
  anstable[i] = read()
end
thedriver87 #8
Posted 22 June 2013 - 06:51 PM
i was in facnt just using an example and the purpose of using the X is the user defines how many are being written so its not a static number of for loops. i was wondering what the whole i,v in pairs was about or what purpose it serves in place of my code..
theoriginalbit #9
Posted 22 June 2013 - 07:38 PM
Ok so ipairs/pairs what is it….. well this has been typed loads of times on these forums, lets just do links this time ;)/>

Official reference:
http://www.lua.org/pil/7.2.html
http://www.lua.org/pil/7.3.html

Another "Official" reference:
http://lua-users.org.../TablesTutorial

A bunch of topics on these forums that Google brought up when I searched (oddly all answered by me):
http://www.computerc...-ipairsgems-do/
http://www.computerc...rs-doesnt-work/
http://www.computerc...r-iv-in-ipairs/
johnnic #10
Posted 23 June 2013 - 12:11 AM
Try using table.insert(tableName, string). It will create one new entry at the end of the table.
thedriver87 #11
Posted 23 June 2013 - 03:05 PM
so after banging my head on the laptop several times during reading BIT's links what i have taken away is…
for 1,table do
end
is the same as using the ipairs(table) and will only call up indexed numerical values
while the in pairs(table) will call up every value even if its not an indexed value ie a string.
now i suppose my question now is if there are numerical values and strings in the table will it call them up in the order stored or the indexed values first then alphabeticly?
Engineer #12
Posted 23 June 2013 - 03:27 PM
It will first do the indexed values in order, but after that the string keys are not alpabetically sorted.
theoriginalbit #13
Posted 23 June 2013 - 04:41 PM
As Engineer stated there is no order, however by writing our own function we can bring order.
http://www.computercraft.info/forums2/index.php?/topic/13618-in-pairs-gives-strange-ordering/page__pid__127913#entry127913
thedriver87 #14
Posted 24 June 2013 - 04:16 PM
thanks for the explanations everyone it has been most helpful!