Jump to content




Multiple word arguments?


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

#1 BrickBuilder212

  • New Members
  • 2 posts

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


#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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


#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 26 February 2015 - 07:26 PM

for the space calculation use

space = math.floor((39 - length) / 2)


#4 BrickBuilder212

  • New Members
  • 2 posts

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


#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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+.

#6 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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 by Dragon53535, 27 February 2015 - 01:16 AM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users