26 posts
Location
Southern Oregon
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?
8543 posts
Posted 22 June 2013 - 03:39 PM
You're redeclaring the table each time, so only the last string will be in the table.
26 posts
Location
Southern Oregon
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.
808 posts
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
26 posts
Location
Southern Oregon
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
26 posts
Location
Southern Oregon
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?
1688 posts
Location
'MURICA
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
26 posts
Location
Southern Oregon
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..
7508 posts
Location
Australia
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.htmlhttp://www.lua.org/pil/7.3.htmlAnother "Official" reference:http://lua-users.org.../TablesTutorialA 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/
53 posts
Location
Somewhere in ****** County, *******
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.
26 posts
Location
Southern Oregon
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?
1522 posts
Location
The Netherlands
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.
7508 posts
Location
Australia
Posted 23 June 2013 - 04:41 PM
26 posts
Location
Southern Oregon
Posted 24 June 2013 - 04:16 PM
thanks for the explanations everyone it has been most helpful!