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

concatenate table and string

Started by Linrox, 12 September 2016 - 01:57 PM
Linrox #1
Posted 12 September 2016 - 03:57 PM
i am trying to use a name given by the user via io,read() as the name to be wrapped.

for example

newDevName=io.read()
newDevName=peripheral.wrap(DevName)

i have also tried using tostring(newDevName) but still get "attempt to concatenate table and string"
Is their a work around for this.

if more info is needed please let me know.
KingofGamesYami #2
Posted 12 September 2016 - 05:20 PM
That code will not give the error you specified. Please post the code that throws that error.
CCJJSax #3
Posted 12 September 2016 - 06:17 PM
Yeah more info is definitely needed. In that code DevName is not defined so that would throw an attempt to call nil, but not that error.
KingofGamesYami #4
Posted 12 September 2016 - 07:57 PM
Yeah more info is definitely needed. In that code DevName is not defined so that would throw an attempt to call nil, but not that error.

It would not throw attempt to call nil. It would simply not wrap the peripheral.
Linrox #5
Posted 13 September 2016 - 02:59 AM
Here is an image of the error


@kingofGamesYami
The error is not saying it concatenated nil with anything.
and also the image shows the error message.

and the full code
http://pastebin.com/TZg9qyyk
CCJJSax #6
Posted 13 September 2016 - 03:08 AM
Really? I learned something.
Linrox #7
Posted 13 September 2016 - 03:31 AM
ok i fixed my issue, and improved functionality and shortened the code

Thanks for your help
TheRockettek #8
Posted 13 September 2016 - 07:45 AM
your printing peripheral.wrap, which returns a table. If your trying to get a name of a peripheral use peripheral.getType()

67. newDevName=peripheral.wrap(DevName)
68. print(newDevName.." Assigned")
KingofGamesYami #9
Posted 13 September 2016 - 12:18 PM
For anyone reading this in the future, wrapped peripherals are tables and cannot be concatenated with a string, which Linrox attempted to do on line 67.
Linrox #10
Posted 14 September 2016 - 04:31 AM
I didn't need to change the line at all it now works. I only had to move it into the k,v loop above it. Moved lines 65 & 66 up to line 63 before the end statement.
Here is the updated version. But still heavily a WIP.
TheRockettek #11
Posted 14 September 2016 - 07:25 AM
Its coz you changed newDevname assigned to v assigned

also another thing, you can do peripheral.getSides() for a list of connected peripherals. (Its better as it can also detect stuff connected via rednet cables)

Also what is your program meant to do? xD