Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Ieldra

Problem with writing a text file and functions that return arrays

Question

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
end

f.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.

 

 

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0
(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

end

f.close()

That is not valid Lua code (you have one end too much). Can you post the full code that's causing the problem? Also by the way \n\r is not a valid line terminator. On Unix-like systems (including OC) you'd just use \n, and the Windows default is \r\n.

 

(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.

Arrays in Lua are indexed starting at 1. Lua doesn't have a dedicated array type, it only has table, which is a mapping/dict/object/whatever, so accessing element 0 doesn't produce an error.

 

(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.

In the OpenOS shell you can use normal Unix pipes, like mycommand > output.txt.

Link to post
Share on other sites
  • 0

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.

Link to post
Share on other sites
  • 0

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_

 

foutput.jpg

 

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?
 

Link to post
Share on other sites
  • 0

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)

Link to post
Share on other sites
  • 0

In OpenOS 1.6 the piping system was greatly improved, and is capable to redirect not only io.stdxxx buffers, but also print and term.write.

So, to be able to redirect the output of your program, you should consider either upgrading OpenComputers, or using io.write instead of print.

 

And the fs issues... I think your test program should look like this:

local fs = require("filesystem")
local f = fs.open("/testfile.txt", "w")
print(f)
print(f:write("testtext")) -- colon, not period
f:close() -- and here, too

But why do you not just use io.open? You don't need to require any library, and it writes into the internal buffer, which flushes on close or by using a :flush, as the raw fs handle writes directly onto the disk, and, obviously, takes more time.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.