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

Quick question about paths and concatenation.

Started by RockLegend2, 22 May 2013 - 06:25 AM
RockLegend2 #1
Posted 22 May 2013 - 08:25 AM
Hey, guys.

This ought to be an easy question for a pro.

Can you concatenate a variable with a string to create a path?

Example:
zipcode = 1234
fs.open("disk/" .. zipcode, "r")


Would that work and open "disk/1234"?
H4X0RZ #2
Posted 22 May 2013 - 08:32 AM
Yes that works with tostring()

local zipcode = 1234 --here is the numeric variable
local file = fs.open("disk"..tostring(zipcode), "r") --tostring converts zipcode into a string
RockLegend2 #3
Posted 22 May 2013 - 08:35 AM
Thank you very much.
Kingdaro #4
Posted 22 May 2013 - 12:02 PM
For the record, numbers are automatically converted to strings when concatenated.


local num = 5
local str = 'hello'

print(str .. num) --> hello5
print(num .. num) --> 55
nateracecar5 #5
Posted 22 May 2013 - 09:29 PM
Your code worked when you first created it. If not, it is a bug. Like Kingdaro said, numbers are automatically "tostring'ed" if you will when concatenated.