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

Multiple word arguments?

Started by BrickBuilder212, 26 February 2015 - 05:08 PM
BrickBuilder212 #1
Posted 26 February 2015 - 06:08 PM
I am trying to make a program that can print an ascii art logo then a message underneath e.g:

   ASCIILOGO
  Test message

I have made it so that it prints correctly but I cant figure out how to run the program with a multiple word argument

So far I have:

--Variables
local message = ...
local line = ""
local length = string.len(message)
local space = 0
local before = ""

--Calculate number of spaces before message to centre
if (length % 2 == 0) then
  space = ((39 - length) / 2)
else space = (((39 - length) / 2) - 0.5
end

--Change a number of spaces into a string of that number of spaces
for x = 1, space, 1 do
  before = before .. " "
end

*loads of print()s to print the logo*

print(before .. message)

And in another program:

shell.run("Logo","Test message")

This code works fine with no errors but only prints the first word of the message so it would print:

ASCIILOGO
   Test
Cranium #2
Posted 26 February 2015 - 07:06 PM
You can put the arguments into a table, and you can just reference the different indices of the table.

local args = {...}
for i = 1, #args do
    print(args[i])
end
Creator #3
Posted 26 February 2015 - 07:26 PM
for the space calculation use


space = math.floor((39 - length) / 2)
BrickBuilder212 #4
Posted 26 February 2015 - 08:00 PM
Thank you for your help

I did:

local args = {...}

for i = 1, #args do
  message = message .. " " ..args[i]
end
Lyqyd #5
Posted 26 February 2015 - 08:32 PM
Be aware that you'll need to initialize your variable as an empty string first, or else it will be attempting to concatenate a string and a nil, which will throw an error. You may want to actually initialize it as args[1] (after first checking that it exists), then if args[2] exists, run the loop to add the rest of the arguments, and simply have it start at 2.

Of course, if you're using a more modern version of ComputerCraft, this should work too, when running from the shell:


> myprog "argument argument"

That should show up as a single argument when running the program on anything recent. I want to say 1.6+, but I'd have to check to be sure. Definitely is present in 1.65+.
Dragon53535 #6
Posted 27 February 2015 - 01:16 AM
If you just want it to be a phrase you can easily save the args to a table the concat said table into a single string with spaces, as such:

local tbl = {"I'm","how","arguments","would","show","up","in","the","table"}
print(table.concat(tbl," ") --#The string space is a separator.
--#>I'm how arguments would show up in the table
--#Without the separator
--#>I'mhowargumentswouldshowupinthetable
Edited on 27 February 2015 - 12:16 AM