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

Connect number to string

Started by The Penguin21, 05 November 2017 - 12:03 PM
The Penguin21 #1
Posted 05 November 2017 - 01:03 PM
Hello. I am working currently on a project, which opens specific channels on specific a modem.
I have wrapped a modem to modem1, modem2, modem3, modem4, modem5, and modem6. How can I connect the string "modem" to a variable that holds the number that I want to add to the end of string? The code looks something like this:

for z=1,6 do
  "modem"..z.open(x) --Help me with this line
end
In this case I am trying to open the channel x on each modem.
KingofGamesYami #2
Posted 05 November 2017 - 01:44 PM
That is not how variables work.

I suggest you use a table.
Dog #3
Posted 05 November 2017 - 05:30 PM
Bulding a little on KoGY's answer:

Variables and strings are two different things. When you enclose something in quotes it becomes a string. A variable can hold (or in CC's case, point to) a string, but a string is just a string.

Depending on what version of CC you're using, you could do something like this…

local modem = { peripheral.find("modem") } --# capture all found modems in a table call 'modem'
local x = 2345
for i = 1, #modem do --# start a loop that will count through the number of modems
  modem[i].open(x) --# open channel 'x' on each modem (e.g. modem[1], modem[2], etc.)
end

The Penguin21 #4
Posted 06 November 2017 - 12:47 PM
Thanks, I will try that.