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

[Solved] attempt to concatenate string and nil

Started by moneymaster2012, 06 July 2017 - 03:28 AM
moneymaster2012 #1
Posted 06 July 2017 - 05:28 AM
I'm pretty sure it's something wrong with how I'm using the FS api.


shell.run("clear")
print("Lazuli Azure Resort and Spa")
print("---------------------------")
print(" ")
function pickRoom()
  room = {101, 102, 103, 201, 202}
  a = math.random(0,4)
  numbRoom = tonumber(room[a])
  dir = "rooms/"..numbRoom -- LINE 10
  h = fs.open(dir, "r")
  if h == "true" then
	shell.run("clear")
	print("Your room number is "..numbRoom..".")
	local g = read()
	h = fs.open(dir, "w")
	h.write("false")
	h.close()
	pickroom()
  else
	pickRoom()
  end
end
pickRoom()

Returns startup:10: attempt to concatenate string and nil

Anybody have a fix?
Edited on 06 July 2017 - 04:43 AM
KingofGamesYami #2
Posted 06 July 2017 - 05:58 AM
Well, it's not the fs API. That won't throw that particular error. "concatenate" refers to the joining of two strings (the .. operator).
I'd say for whatever reason numbRoom is nil. Probably a misspelling, check lines 9 and 10. SquidDev is correct. (You also appear to have a misspelling later, calling "pickroom" not "pickRoom")
—–
I'd also like to point out that this function is endlessly-recursive which will lead to problems. Instead of having pickRoom call itself, you should use a loop of some kind!
The FS API bit also won't work – a file handle will never equal a string. You'll want h.readAll() == "true"
You could also improve by localizing more of your variables – be careful of scope issues!
shell.run("clear") is just plain bad practice, it's easily replaced by a call to term.clear and term.setCursorPos.
Edited on 06 July 2017 - 04:05 AM
SquidDev #3
Posted 06 July 2017 - 06:03 AM
Lua, for various reasons starts arrays at 1, rather than 0. This means your call to random must be between 1 and 5, instead of 0 and 4.
moneymaster2012 #4
Posted 06 July 2017 - 06:04 AM
Well, it's not the fs API. That won't throw that particular error. "concatenate" refers to the joining of two strings (the .. operator).

I'd say for whatever reason numbRoom is nil. Probably a misspelling, check lines 9 and 10. (You also appear to have a misspelling later, calling "pickroom" not "pickRoom")

—–
I'd also like to point out that this function is endlessly-recursive which will lead to problems. Instead of having pickRoom call itself, you should use a loop of some kind!
The FS API bit also won't work – a file handle will never equal a string. You'll want h.readAll() == "true"
You could also improve by localizing more of your variables – be careful of scope issues!
shell.run("clear") is just plain bad practice, it's easily replaced by a call to term.clear and term.setCursorPos.

Haha I cringe the same way you do when I see shell.run("clear") and using functions as loops.

Forgot to change this function back to a loop - I was just trying to see if the loop was the issue.

And I didn't feel like writing one more line of code for a clear function.

I am not new to lua or programming in general. Don't worry, I know better.
Dog #5
Posted 06 July 2017 - 06:04 AM
A couple of things I noticed:
1. You're indexing your table starting with 0, in Lua you'll want to start with 1 unless you specifically create a 0 based entry.
2. You're attempting to concatenate a string and number, which I don't *think* will work. You're probably getting the error you're getting because your number is nil. I believe you'll need to tostring() your number before concatenating.

EDIT: :ph34r:/> 'd
Edited on 06 July 2017 - 04:07 AM
moneymaster2012 #6
Posted 06 July 2017 - 06:06 AM
deleted

A couple of things I noticed:
1. You're indexing your table starting with 0, in Lua you'll want to start with 1 unless you specifically create a 0 based entry.
2. You're attempting to concatenate a string and number, which I don't *think* will work. You're probably getting the error you're getting because your number is nil.

EDIT: :ph34r:/> 'd

I was taught to always start with 0 in java, so it's just a habit. Thanks for the heads up.
Edited on 06 July 2017 - 04:05 AM
moneymaster2012 #7
Posted 06 July 2017 - 06:22 AM
Well, it's not the fs API. That won't throw that particular error. "concatenate" refers to the joining of two strings (the .. operator).
I'd say for whatever reason numbRoom is nil. Probably a misspelling, check lines 9 and 10. SquidDev is correct. (You also appear to have a misspelling later, calling "pickroom" not "pickRoom")
—–
I'd also like to point out that this function is endlessly-recursive which will lead to problems. Instead of having pickRoom call itself, you should use a loop of some kind!
The FS API bit also won't work – a file handle will never equal a string. You'll want h.readAll() == "true"
You could also improve by localizing more of your variables – be careful of scope issues!
shell.run("clear") is just plain bad practice, it's easily replaced by a call to term.clear and term.setCursorPos.
A couple of things I noticed:
1. You're indexing your table starting with 0, in Lua you'll want to start with 1 unless you specifically create a 0 based entry.
2. You're attempting to concatenate a string and number, which I don't *think* will work. You're probably getting the error you're getting because your number is nil. I believe you'll need to tostring() your number before concatenating.

EDIT: :ph34r:/> 'd

New code (with your suggestions and removed laziness)


term.clear()
term.setCursorPos(1,1)
print("Lazuli Azure Resort and Spa")
print("---------------------------")
print(" ")
while true do
  room = {101, 102, 103, 201, 202}
  a = math.random(1,5)
  numbRoom = tostring(room[a])
  dir = "rooms/"..numbRoom
  h = fs.open(dir, "r")
  if h == "true" then
	term.clear()
	term.setCursorPos(1,1)
	print("Your room number is "..numbRoom..".")
	local g = read()
	h = fs.open(dir, "w")
	h.write("false")
	h.close()
	break
  end
end
os.shutdown()

Too tired to wrap my brain around loops and stuff. When entering the loop, nothing prints and discards if statement contents until the computer crashes.

Any solutions?


EDIT: Basically, if the room is unavailable ("false", not "true"), then I want it to keep looping until it finds an available room.
Edited on 06 July 2017 - 04:24 AM
moneymaster2012 #8
Posted 06 July 2017 - 06:43 AM
Found solution. Needed to add readLine()