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

Variable in a path

Question

Hey there!

I'm working on a program similar to the database but I've find out problem I couldn't figure out...

Program should ask for code name (which'll be file name), information about code name, date and time of saving etc. But how I can insert variable in io.open()? Code now looks like this:

...
print("Enter command:")
print("")
if io.read() == "add" then
print("Enter item location:")
LOC = io.read()
f = io.open("/WH/db/"LOC, "w") -- This is the problem, LOC (variable, as seen above) should be future file name...
print("Enter information:")
CONT = term.read()
f:write(CONT)
f:close()
...

Thanks for help,

Steve505

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Oh... That's pretty easy, I was expected something more difficult.

Thank you for help, code is now working perfectly.

Steve505

EDIT: I've found another two problems:

  1. Can I use io.open():read() only on specific line of a file?
  2. Is there a way of printing io.open():read() without quotations? E.g. when the file contains text She sells sea shells..., mentioned code'll print on the screen "She sells sea shells", and I want to remove these quotation marks.
Link to post
Share on other sites
  • 0

You can use io.open(path):read() to read a specific line, but only indirectly. But it might be easier to use the iterator io.lines(path).

-- # use io.read to get the whole file, it returns a string; then use string.gmatch/string.match to extract the specific line
-- # Examples:
-- # Ex1: get line 5
local path = "/somepath"
local count = 1
local targetline = 5
local file = io.open(path):read("*all")

for line in file:gmatch("[^\n]+") do
  if count == targetline then return line end
  count = count + 1
end

-- # Ex2: find line starting with #
local path = "/somepath"
local indicator = "#"
local file = io.open(path):read("*all")

return file:match("["..indicator.."][^\n]+")

-- # io.lines iterator, returns new line whenever it's called
-- # Examples:
-- # Ex1: get line 5
local path = "/somepath"
local count = 1
local targetline = 5

for line in io.lines(path) do
  if count == targetline then return line end
  count = count + 1
end

-- # Ex2: find line starting with #
local path = "/somepath"
local indicator = "#"

for line in io.lines(path) do
  if line:sub(1, #indicator) == indicator the return line end
end

 

I only got quotation marks in the  string returned by io.open():read() when I just ran io.open():read() in the Lua prompt; when I used print(io.open():read()) or io.write(io.open():read()) it printed the string without quotation marks in the output.

 

Link to post
Share on other sites
  • 0

I would not recommend doing io.open():read() or write(), you are never closing the file so you can't guarantee the handle is released. Make a custom function to readall/writeall quickly if you need to.

Also filesystem.concat might be better than just using "WH/db/"..LOC

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.