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

Need help

Started by MetalMiner, 24 July 2012 - 06:39 PM
MetalMiner #1
Posted 24 July 2012 - 08:39 PM
Hi there

I hope i can explain my problem in english :)/>/>

I want to print a value, stored in a table.
But the place, where it's stored should base on a string.

Example:
 
local actualLine = 1                                                    --the line to print
local lines = {                                                         --all lines
              line1 = {text = "exampletext1", length = 12},
              line2 = {text = "exampletext2", length = 12},
              }

s = "lines.line"..actualLine..".text"

print(???)                                                              -- no i want to print the value stored in the table, with the "adress" s

Hope, you understand the most and can help me :o/>/>

Greetings
MetalMiner
Grim Reaper #2
Posted 24 July 2012 - 10:22 PM
To print all of the values in your table, if I understand your question correctly, you could iterate or go through all of the values and print them out with a simple loop:


for i,v in pairs( lines ) do print( v.text ) end

In this case, your storing tables inside a table. Each of the tables within the 'lines' table has two variables: the text and the length.
You reference variables within a table with the '.' separator.

For example:

local t = { text = "Hey!", length = 4 }

print( t.text )
print( t.length )

The code above would print "Hey!" and "4".

Look at the Lua manual's guide through tables: Lua wiki: tables.

Hope I helped! :)/>/>
MetalMiner #3
Posted 24 July 2012 - 11:49 PM
Thank you for your quick answer.
But i didn't explain good enough.
I'm making a little text editor.
For this, i want to store all the information about each line of text in a table (start, end, length).
Like this:

local actualLine = 3
lines = {
        line1 = {start = 3, xEnd = 12, length = 9},
        line2 = {start = 3, xEnd = 15, length = 12},
        line3 = {start = 3, xEnd = 14, length = 11}
        }
event, "char" = os.pullEvent()
if event == char then
  write(char)
  --here i want to change the values in the table
end
The variable "actualLine" stores the line, where the cursor is at the moment.
After get the pressed key (os.pullEvent) and print the char, I want to edit the lenght and the end of the line (in the table).
I tried to solve it with the variable actualLine, because i want to make a general code
(not if actualLine == 1 then change values of line 1 elseif actualLine == 2 then …).
But i can't "connect" the table and the actual line.
I tried it with loadstring("lines.line"..actualLine..".start), but it didn't work.

Hope you (or someone else) can help me now and sorry for my bad english :)/>/>
metalminer