RegEx support for peripheral.find()
JWTHDYTWA opened this issue ยท 4 comments
Is it possible to make peripheral.find('')
use regular expressions?
For example if you need to wrap any machine from some mod.
Like peripheral.find('pneumaticcraft:.+')
Lua pattern support would be more likely to happen.
Also, peripheral.find
has the option to pass a filter function which might be able to solve the same problem.
If peripheral.find
cant do it then peripheral.getNames
with peripheral.wrap
will. Although maybe peripheral.getType
would be better as the peripheral might not be on a network and thus not have a name matching it's type.
Lua pattern support would be more likely to happen.
Patterns are kind of regex implementation in Lua, so that's exactly what i meant.
Also,
peripheral.find
has the option to pass a filter function which might be able to solve the same problem.
It won't work like that because filter function works only over one exact type of peripherals specified in 1st arg.
If
peripheral.find
cant do it thenperipheral.getNames
withperipheral.wrap
will.
It will, but it would be much more convenient with patterns in peripheral.find
.
You could define your own method and use require
to load it anywhere you want it.
Pull requests are more likely to get results with this kind of thing, and making your own module to do it yourself would get you part way there.
Can you explain what sort of situation where you would use this? My gut feeling here is that finding peripherals via string patterns isn't a pattern (heh) we want to encourage, but I might be wrong! Could you post the code where you're planning to use this?
I guess as a stop gap, you can probably copy-and-paste the following into your code:
local function find_pat(pat)
local results = {}
for _, name in ipairs(peripheral.getNames()) do
for _, ty in ipairs({peripheral.getType(name)}) do
if ty:find(pat) then
table.insert(results, peripheral.wrap(name))
break
end
end
end
return table.unpack(results)
end