Combining Tables
#1
Posted 03 August 2015 - 07:52 PM
I was just about to start programming a program that would send messages and the message comes from the parameters, when I realised, how do I make it combine multiple arguments into one to then send the message off in one string?
So, my question is, how do I make it combine multiple arguments into one to then send the message off in one string?
Thanks in advanced,
_Zircon_
#2
Posted 03 August 2015 - 08:09 PM
--# Just an example table local t = { "hello", "world", "foo", "bar" } local text = "" --# This would be the string you'd send for i = 1, #t do --# Loop through all the other strings text = text .. t[i] --# Concatenate the strings end print( text ) --# Outputs: helloworldfoobar
Do you have any code? If so, then please post it, as it would be helpful for a better understanding of what you mean.
#3
Posted 03 August 2015 - 08:20 PM
TheOddByte, on 03 August 2015 - 08:09 PM, said:
--# Just an example table local t = { "hello", "world", "foo", "bar" } local text = "" --# This would be the string you'd send for i = 1, #t do --# Loop through all the other strings text = text .. t[i] --# Concatenate the strings end print( text ) --# Outputs: helloworldfoobar
Do you have any code? If so, then please post it, as it would be helpful for a better understanding of what you mean.
Thank you !
_Zircon_
#6
Posted 03 August 2015 - 08:33 PM
I have used your code, but it keeps erroring with "chatbox:18: attempt to concatenate nil and string"
The Code:
local tArgs = {...} local function printUsage() printError("Usages:") printError("chatbox say <msg>") printError("chatbox tell <to-player> <msg>") return end if #tArgs < 1 then printUsage() return end local sCommand = tArgs[1] if sCommand == "say" then for i = 3, #tArgs do --starting from 3 because the msg starts from the 3rd value. text = text.." "..tArgs[i] end print( text ) elseif sCommand == "tell" then printError("Not yet implemented!") return else printError("Unrecognized command") printUsage() return end
Please help me with this!
_Zircon_
#7
Posted 03 August 2015 - 08:46 PM
#8
Posted 03 August 2015 - 08:46 PM
_zircon_, on 03 August 2015 - 08:33 PM, said:
for i = 3, #tArgs do --starting from 3 because the msg starts from the 3rd value. text = text.." "..tArgs[i] endwouldn't that part be this instead
for i = 2, #tArgs do text = text .. " " .. tArgs[i] endbecause the usage is "say <message>" and not "say <parameter> <message>"
edit: ninja'd
Edited by TheOddByte, 03 August 2015 - 08:47 PM.
#9
Posted 03 August 2015 - 08:49 PM
KingofGamesYami, on 03 August 2015 - 08:46 PM, said:
TheOddByte, on 03 August 2015 - 08:46 PM, said:
_zircon_, on 03 August 2015 - 08:33 PM, said:
for i = 3, #tArgs do --starting from 3 because the msg starts from the 3rd value. text = text.." "..tArgs[i] end
wouldn't that part be this instead
for i = 2, #tArgs do text = text .. " " .. tArgs[i] endbecause the usage is "say <message>" and not "say <parameter> <message>"
edit: ninja'd
I changed it to
for i = 2, #tArgs dobut that didn't change anything and it is still erroring with the exact same error.
_Zircon_
#10
Posted 03 August 2015 - 11:03 PM
elseif sCommand == "tell" then
That line couldn't throw the error you got, are you sure this is the exact code you ran?
I think its actually this line that errors:
text = text.." "..tArgs[i]The first time that is executed, 'text' hasn't been defined yet
Therefore it errors, as you are trying to combine 'nil' and whatever 'tArgs[i]' is.
You just need to define 'text' as a blank string:
if sCommand == "say" then local text = "" for i = 3, #tArgs do text = text.." "..tArgs[i] end print( text ) elseif sCommand == "tell" then
#11
Posted 04 August 2015 - 12:04 AM
for i = 2, #tArgs doshould still apply, because varargs doesn't take the program name as an argument, so the message would be the second argument in your case
local tArgs = {...} if #args < 1 then error( "Usage: chatbox <text>" ) --# In this case "chatbox" would be the name of your program else print( args[1] ) --# But when printing the first argument in the varargs table it prints whatever "<text>" was, and not "chatbox" end
#12
Posted 04 August 2015 - 12:52 AM
Remember you can do this:
local t = {"hello", "world", "foo", "bar"} rednet.send(id, t)
local t = rednet.receive() for i = 1, #t do print(t[i]) end
Also, don't forget that table.concat() is a thing:
print( table.concat( tArgs, " ", 2 ) )
#13
Posted 04 August 2015 - 09:22 AM
Bomb Bloke, I am trying to create a program that will PM people with a message or broadcast a message, using Chatboxes from MoarPeripherals.
#14
Posted 04 August 2015 - 10:37 AM
#15
Posted 05 August 2015 - 06:42 AM
_zircon_, on 04 August 2015 - 10:37 AM, said:
When you say 'label', do you mean the name of the chat box?
I didn't know you could set the name, but the mod I used to use may have updated, or you might be using a mod I don't know about.
Anywho, it shouldn't really matter, I just wanted to know.
Some preliminary sudo-code:
So with that in mind:
tArgs = {...} local labelPos = #tArgs+1 --# A variable to store the position of "-l" - It defaults to the length of tArgs for i = 3, #tArgs do --# Start from 3, 1 is "say" and 2 is the message - lets assume the message has to be at least 1 word if tArgs[i] == "-l" then labelPos = i break end end local msg = {} --# Stores the message local label = {} --# Stores the label, if there was one for i = 2, labelPos-1 do --# Continue until one before "-l", or the end of tArgs if we didn't find "-l" msg[ i-1 ] = tArgs[i] --# Copy the message from tArgs to msg end msg = table.concat(tArgs , " " , 2 , labelPos-1) label = table.concat(tArgs , " ", labelPos+1 , #tArgs) --# Note the second one shouldn't do anything if "-l" wasn't foundWhen this code has finished, 'label' and 'msg' will each be a string.
Tested, it works as intended
Any questions feel free to ask
Edited by HPWebcamAble, 06 August 2015 - 04:35 PM.
#16
Posted 06 August 2015 - 06:29 AM
local msg = {} --# Stores the message for i = 2, labelPos-1 do --# Continue until one before "-l", or the end of tArgs if we didn't find "-l" msg[ i-1 ] = tArgs[i] --# Copy the message from tArgs to msg end msg = table.concat( msg , " " )
... you could instead just do:
local msg = table.concat( tArgs, " ", 2, labelPos - 1 )
#18
Posted 08 August 2015 - 01:44 PM
#19
Posted 08 August 2015 - 01:50 PM
_Zircon_
#20
Posted 09 August 2015 - 01:59 AM
_zircon_, on 08 August 2015 - 01:50 PM, said:
So did you see my example above?
If run as a program, it separates the arguments into the 'msg' and 'label' variable.
Do what you will with it.
For the 'tell' functionality, you'll need to accept another argument, the player to tell.
If you are happy with the way it works, then great, but personally make the program work like this:
(Assuming the program is called 'chatbox')
chatbox say <label> <message> chatbox tell <player> <label> <message>
Remember that you can do this in the shell:
chatbox tell bob "Chat Box" "hi bob"The quotes (") makes the shell pass the contents as one argument.
Eg:
print( textutils.serialize( tArgs ) ) --> { "tell", "bob", "Chat Box", "hi bob", }
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users