CC: Tweaked

CC: Tweaked

59M Downloads

RegEx support for peripheral.find()

JWTHDYTWA opened this issue ยท 4 comments

commented

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:.+')

commented

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.

commented

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 then peripheral.getNames with peripheral.wrap will.

It will, but it would be much more convenient with patterns in peripheral.find.

commented

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.

commented

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