Jump to content




Question about printing variables


  • You cannot reply to this topic
2 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

Posted 08 May 2013 - 05: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

#2 Shnupbups

  • Members
  • 596 posts
  • LocationThat place over there. Y'know. The one where I am.

Posted 08 May 2013 - 05: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.

#3 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 08 May 2013 - 06: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. :)/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users