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

converting file to tables

Started by chalenged, 15 September 2012 - 03:19 AM
chalenged #1
Posted 15 September 2012 - 05:19 AM
Hello, i've been working on a printer using redpower 2 frames and an assembler (the cable on back is for frames, bottom is assembler), and i have it printing well from an array, however i need to be able to take a text file like so(creeper face):

hhhhhhhh
hhhhhhhh
hkkhhkkh
hkkhhkkh
hhhkkhhh
hhkkkkhh
hhkkkkhh
hhkhhkhh
and have that put into the array in my code. Right now i have it set to the colors in order. Here is the code(testcolors is the array): http://pastebin.com/jLyMJ7pg
thanks in advanced.
KaoS #2
Posted 15 September 2012 - 06:02 AM
Try this :)/>/>



local function tolines(filename)
  local intdone=nil
  local oFile=io.open(filename,'r')
  local tLines={}
  while true do
	local line=oFile.readLine()
	if not line then
      oFile.close()
	  break
	end
	table.insert(tLines,line)
  end
  return tLines
end

local function tochars(line)
  local tChars={}
  for i=1,#line do
	table.insert(tChars,string.sub(line,i,i))
  end
  return tChars
end


local tFile={}
for k,v in pairs(tolines(yourfilename)) do
  table.insert(tFile,tochars(v))
end

tFile is then your array

I also took a look at your printing code, take a look at this one, I included comments

EDIT: I do not know how you have set up your redpower builder so I just hope this works, if it builds from a crane (as in places downwards) then you will need to reverse the vertical placing order or it won't work
Edited on 15 September 2012 - 04:25 AM
Lyqyd #3
Posted 15 September 2012 - 06:16 AM
This would take a file in that format as the argument to the program, such as <programname> <filename>, replacing each with the actual names, of course. I also cleaned up a few things.


local args={ ... }
local input = {}
if #args < 1 then
    print("Please specify a file")
else
    file = io.open(args[1])
    if file then
        for line in file:lines() do
            local inputLine = {}
            for char in string.gmatch(line, "%a") do
                table.insert(inputLine, char)
            end
            table.insert(input, inputLine)
        end
        file:close()
    end
end

local colorTable = {q=0,w=1,e=2,r=3,t=4,y=5,u=6,i=7,a=8,s=9,d=10,f=11,g=12,h=13,j=14,k=15}

function Place(key)
    local color
    if colorTable[key] then
        color = 2^colorTable[key]
    else
        print("Error: Unrecognized Color!")
    end
    rs.setBundledOutput("bottom",color)
    sleep(.05)
    rs.setBundledOutput("bottom",0)
    sleep(.05)
end
     
function Up()
    rs.setBundledOutput("back", 1)
    sleep(.4)
    rs.setBundledOutput("back", 0)
    sleep(.4)
end
     
function Down()
    rs.setBundledOutput("back", 2)
    sleep(.4)
    rs.setBundledOutput("back", 0)
    sleep(.4)
end
     
function Right()
    rs.setBundledOutput("back", 4)
    sleep(.4)
    rs.setBundledOutput("back", 0)
    sleep(.4)
end
     
function Left()
    rs.setBundledOutput("back", 8)
    sleep(.4)
    rs.setBundledOutput("back", 0)
    sleep(.4)
end

for i=1,8 do
    Up()
end
Right()
Right()
line=0
for i=1,4 do
    line=line+1
    if line~=1 then
        Down()
        Right()
    end
    for i=1,8 do
        Place(input[line][i])
        Right()
    end
    Down()
    line=line+1
    for i=8,1,-1 do
        Place(input[line][i])
        Left()
    end
end
Left()
Down()
KaoS #4
Posted 15 September 2012 - 06:24 AM
I like the incorrect syntax catching and I did forget to oFile.close() in my code, adding it now
Lyqyd #5
Posted 15 September 2012 - 06:48 AM
I like the incorrect syntax catching and I did forget to oFile.close() in my code, adding it now

Also, you'd need to change io.open to fs.open, since you seem to be using the fs API in all your other file operation calls.
chalenged #6
Posted 15 September 2012 - 06:58 AM
Awesome i got it working, thanks for the help guys. I'll make a video and refine the code later(i'll give credit), but for now it's working great. The input code was actually there from when i was debugging the arm movement.