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

Question about printing variables

Started by grand_mind1, 08 May 2013 - 04:31 PM
grand_mind1 #1
Posted 08 May 2013 - 06:31 PM
I know you can print variables using

print("Stuff: ",variable)
and

print("Stuff: "..variable)
I was wanting to know the difference between the two. When trying to do something like this:

floor = 1
print("Floor: ",floor)
It just prints "Floor: " and doesn't print the variable. But when using this:

floor= 1
print("Floor: "..floor)
It works fine. I don't know if it has to do with it being a number but I just wanted to know the difference so I can use the correct one the first time
Help is appreciated!
Thanks! :D/>
Shnupbups #2
Posted 08 May 2013 - 06:34 PM
Commas make them separate arguments. AFAIK print only allows strings to be printed in arguments other than the first. I don't know why, correct me, someone, if I'm wrong.
Kingdaro #3
Posted 08 May 2013 - 07:18 PM
The two dots are used to put strings together. When you use two dots, you smash two (or multiple) strings together and give them all to print() as one string.

myString = "hello" .. " " .. "world"
print(myString)
Is the same as

print("hello" .. " " .. "world")
Both print "hello world".

This can be used for a lot of purposes, such as adding "woop" to whatever the user types:

while true do
  input = read()
  print(input .. " woop")
end

When you use a comma, it sends multiple strings to the print function. The comma is used in more than just the print function with strings, even in default CC functions.

term.setCursorPos(1, 1) -- sending 1 and 1

redstone.setOutput('back', true) -- sending 'back' and true

print("hello ", "world") -- sending "hello " and "world"

The print function is just another one of the functions that can take multiple variables, all it does is smash them all together, like you would do with "..".

Even more uses for the comma:

-- defining variables in a table
myTable = {1, 2, 3, 'a', 'b', 'c'}

-- setting multiple variables
a, b = 4, 2
-- is the same as
a = 4
b = 2

-- getting multiple values from a function
event, param1, param2, param3 = os.pullEvent()

Hope I helped. :)/>/>