- Sky
- Blueberry
- Slate
- Blackcurrant
- Watermelon
- Strawberry
- Orange
- Banana
- Apple
- Emerald
- Chocolate
- Charcoal
Ieldra
-
Content Count
4 -
Joined
-
Last visited
Posts posted by Ieldra
-
-
All right. More testing.
Regarding problem (1): This code....
local fs=require("filesystem") local f=io.open("/testfile.txt","w") print(f) print(io.output(f):write("testtext\r\n")) io.close(f)
...works perfectly fine. The version using filesystem.open and file.write doesn't. It produces an empty file. And rerouting the program's output with ">" likewise produces an empty file.
Regarding problem (2):
This code looks for ReactorCraft hydrogen preheaters and lists their coordinates:
local p = {} local co = require("component") local n = 0 for k,v in co.list() do if string.find(v,"Preheater") ~= nil then n = n + 1 p[n] = k end end print(tostring(n).." ".."Preheaters found:") for i = 1, n do print("Preheater #"..tostring(i)..": "..p[i]) ph = co.proxy(p[i]) print(ph.getCoords()) end
It produces the following output_
Now, the question: how do I access the single x,y,z coordinate values? I haven't found anything that works so far. The "type" function returns "number", and consequently I get an error message about trying to index a number when I try to treat it as a table.
Edit:
I've checked Reika's code, and he sets the return type for getCoords to ARRAY, so I should be able to index it, but I can't. Why would that be?
-
Here's a simple test program:
local fs=require("filesystem")
local f=fs.open("/testfile.txt","w")
print(f)
print(f.write("testtext"))
f.close()This produces the following output:
table <some address> -- from the print(f), so there is a file handle, which means the "open" has worked, right?
nil file is closed -- from the print(f.write...) - well, inexplicably there doesn't seem to be an open file anymore one line later...
followed by an error message that I try to index a nil value (since f has turned inexplicably nil)
Also, if I try to reroute my program's output with > the output file gets created but nothing is ever written into it. It's exactly the same with the file I try to write within the program: the file gets created (with length 0), but nothing is ever written to it.
-
Hi everyone,
I'm just starting out with OpenComputers. I have no problem with the coding, but there are things that don't work where I think they should work:
(1) I'm trying to write a component list to a text file, since the number of components is too large to fit on a screen. I've written this code:
local co=require("component")
local fs=require("filesystem")
local f=fs.open("textfile.txt","w")
--print(f) (commented test line)
for k,v in co.list() do
print(k,v)
print(f.write(k.." "..v.."\n\r"))
end
endf.close()
The file does get opened with the fs.open statement - the commented test line prints a table reference instead of nil, and I find the text file in the OC subfolder of my saved world - but when I try to write with the f.write statement in the loop I get a nil result and the error message "file is closed". For the same reason, the f.close() gives me a an error message. The same happens if I add the test line "print(f.write("test"))" directly after the open statement. What am I doing wrong?
(2) Certain ReactorCraft/RotaryCraft blocks have a method "readtank(<tank index)". This method returns an array with the name/designated fluid type of the tank, its capacity and the amount of liquid in it as its elements, so something like "water 8000 8000" for a full tank of 8B capacity full of water. I can print the complete array with print(readtank(0)), for instance, but I can't access the elements of the array, for instance with
c = readtank(0)
print(c[0])
How do I access the elements of an array returned by a function correctly? print((readtank(0))[0]) doesn't work either.
(3) While I'm at it: Is there a way to route the output of a program to a text file? That way I needn't bother with the filesystem code.
Thanks in advance for any help.
Problem with writing a text file and functions that return arrays
in Programming
Posted
All right, I solved problem (2) on my own. I was unaware of lua's feature to allow more than one variable on the left side of an assignment.
That still leaves my inability to reroute a program's output to a text file using ">", or to write text files with the filesystem.open and file.write functions (see previous posts)