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

string to command

Started by Suppenbiatch, 23 September 2016 - 06:37 PM
Suppenbiatch #1
Posted 23 September 2016 - 08:37 PM
Hi,

I'm currently trying to write a program which shows the name and the qty of items in a deep storage unit on a monitor.

I'm trying to make a universal program so you can connect as many dsu's as you want to…

The problem is my code will make just a make string, that stands for "peripheral.wrap("deep_storage_unit_0")" and not command that i can execute.
How can i change that ?


local dsus = {}
begin = read()
stop = read()
local i = 0

for begin, stop do
dsus[i] = string.format("%s(%q%s", "peripheral.wrap", string.format("%s_%i", "deep_storage_unit",begin),")")
i = i + 1
end

This code will make a table filled with "peripheral.wrap("deep_storage_unit_xx") which I obviously want to use later. (e. g. dsus[2].getStoredItems["qty"])

I know that the read() part isn't good becaus nobody know what to write but it's just a test code so dont worry i'll change that in the future.

Btw, I'm new to lua and all that string.format commands i just googled so i guess there is a far better way to do this, maybe you'll let me know :D/>
Bomb Bloke #2
Posted 24 September 2016 - 06:08 AM
There's usually not much call to try to refer to functions via strings, and there's a much easier way to perform concatenation.

One other way to do things might be:

local dsus = {}

print("Starting value?")
local begin = tonumber(read())

print("Ending value?")
local stop = tonumber(read())

for i = begin, end do
  dsus[i] = peripheral.wrap("deep_storage_unit_" .. i)
end