Jump to content




string to command

peripheral command help

1 reply to this topic

#1 Suppenbiatch

  • New Members
  • 1 posts

Posted 23 September 2016 - 07: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

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 24 September 2016 - 05: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users