Page 1 of 1

how to use string.find on a table together with a regular expression

Posted: Wed Sep 15, 2021 8:25 pm
by coroco
hi, so I have a function that prints modules, the problem is that I don't know what to use to find the module, I tried string find on the table but I get an error "bad argument # 1 to 'find' (string expected, got table)" so what do I need to use to search for a given module?

An example with string.find

Code: Select all

string.find(v,'gam(.*).tmp')
must be with a regular expression because the module suffix is ​​mutable np gamxxxx.tmp

function modules

Code: Select all



function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = ''..k end
         if v == 'Gam4B80.tmp' then print(v)
         end
         s = s .. '['..k..'] = ' .. dump(v)

      end
      return s .. '} '
   else
      return tostring(o)
   end
end

t = enumModules()
d = dump(t)



Re: how to use string.find on a table together with a regular expression

Posted: Thu Sep 16, 2021 7:41 am
by GreenHouse
Like a month ago I did make a DLL's and symbols dumper, and I used string.match for it, so you should be able to make it work that way:

Code: Select all

string.match(StringVariable,'StringToFind')
So, use match to see if it includes 'gam' and '.tmp' in its name, and if so then print it or do what you want to do with it.

Re: how to use string.find on a table together with a regular expression

Posted: Thu Sep 16, 2021 9:14 am
by coroco
GreenHouse wrote:
Thu Sep 16, 2021 7:41 am
Like a month ago I did make a DLL's and symbols dumper, and I used string.match for it, so you should be able to make it work that way:

Code: Select all

string.match(StringVariable,'StringToFind')
So, use match to see if it includes 'gam' and '.tmp' in its name, and if so then print it or do what you want to do with it.
problem with lua engine throws 'string expected, got table' error
Image

Code: Select all

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = ''..k end

         if string.match(v,'gam(.*)') then print(v) end

         s = s .. '['..k..'] = ' .. dump(v)
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

t = enumModules()
d = dump(t)

Re: how to use string.find on a table together with a regular expression

Posted: Thu Sep 16, 2021 9:42 am
by coroco
I managed to solve it, I don't know if code it is fully correct but it works

Code: Select all

function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = ''..k end
         s = s .. '['..k..'] = ' .. dump(v)
         a = tostring(v)
         if string.find(a,'^Gam(.*).tmp') then print(a) end
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

t = enumModules()
d = dump(t)