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

[lua][Question] Is it possible to shorten this?

Started by jetbooster, 19 November 2012 - 05:46 AM
jetbooster #1
Posted 19 November 2012 - 06:46 AM
If i want a computer to print 3 terms, with commas inbetween, is there an easier way than:

print("Current: ",x",",y,",",z)

not paricularly urgent, just thought i must be being inefficient :)/>/>

Thanks
remiX #2
Posted 19 November 2012 - 06:51 AM
Not that i know of. Why does it matter?

You could make the x y z into one variable, I guess?

pos = x .. " " .. y .. " " .. z
print("Current: " .. pos)
billysback #3
Posted 19 November 2012 - 06:55 AM
If you really want to:

print "Current: "..x.." "..y.." "..z

will "," work?, this gets rid of one character presuming "," won't work, I think it does though…

There should be efficiency challenges on the forums, someone gives a bit of code and everyone else tries to make it as efficient as possible, the character count is what the efficiency is measured in :)/>/>
remiX #4
Posted 19 November 2012 - 07:15 AM
I started using CC with Tekkit. I had no clue you could use commas so I have stuck to double dots since :)/>/>
jetbooster #5
Posted 19 November 2012 - 07:24 AM
okay, turning it into a single variable should help, but yeah "," does work.

and it's only a matter of lazyness :)/>/>
Orwell #6
Posted 19 November 2012 - 08:12 AM
Well, if it's just a matter of using frequently in code, I suggest making a little helper function:

local function cur(x,y,z)
  print( string.format("Current: %d,%d,%d", x, y, z) )
end
It still doesn't make the actual function shorter of course. It just makes it shorter to use in the code (supposing that you have to use it like 30 times or so x'D).
Kingdaro #7
Posted 19 November 2012 - 08:17 AM
I was going to suggest something similar to what Orwell suggested, but then I thought about something. Strings can usually have their methods acted upon them, like:this(). I tested if this works with string.format and well,

print(("Current: %d, %d, %d"):format(x, y, z))
This is apparently valid. Apparently.