Jump to content


Photo

convert string to table identifier


    4 replies to this topic

    #1 sdahlka

    sdahlka

      Advanced Member

    • Members
    • PipPipPip
    • 324 posts

      Posted 08 September 2015 - 02:48 AM

      when i input these strings i want it to return a table info of the same name

      example strings:

      "windower.ffxi.get_mob_by_target('t').name"
      "windower.ffxi.get_mob_by_target('t')"
      "windower.ffxi.get_mob_name(1234).name"
      "windower.ffxi.get_mob_name(1234)"
      "windower.ffxi"

      currently the best i can do are these

      example strings:
      "windower.ffxi.get_mob_by_target('t')"
      "windower.ffxi.get_mob_name(1234)"
      "windower.ffxi"

      with this code

       

      function s_to_t(s)--string to table
          local str = s
          local f = (str:match('%(%)') or str:match("%('(.+)'%)") or nil)
          if f then
              str = (f == '()' and string.gsub(str, '%(%)', "") or string.gsub(str, "%('"..f.."'%)", ""))
          end
          local t = {0}
          local tbl = string.find(str,'%.')
          while tbl do
              t[#t+1]=tbl tbl = string.find(str,'%.',tbl+1)
          end
          t[#t+1]=#str+1
          local g = _G
          for i = 1,#t-1 do
              g = g[string.sub(str,t[i]+1,t[i+1]-1)]
          end
          if f then
              if f == '()' then
                  return g()
              else
                  return g(f)
              end
          else
              return g
          end
      end
      local tbl = s_to_t(str)
       

       


      help would be appreciated



      #2 Arcon

      Arcon

        Advanced Member

      • Windower Staff
      • 1189 posts
      • LocationMunich, Germany

      Posted 08 September 2015 - 05:59 AM

      Lua has a built-in eval function called loadstring. You can use it like this:

       

      local target = loadstring('windower.ffxi.get_mob_by_target(\'t\')')()
      print(target.name)


      #3 sdahlka

      sdahlka

        Advanced Member

      • Members
      • PipPipPip
      • 324 posts

        Posted 08 September 2015 - 08:23 AM

        im trying to type in(using a console command for gearswap) the exact table name with out any special chars as to make it as dumb as possible

         

        i.e.

        gs c show var windower.ffxi.get_mob_by_target('t').name

         

        i have this that works for all but the ones with ().name

         

         

                elseif command[1]:lower() == 'show' and (command[2]:lower() == 'variable' or command[2]:lower() == 'var') then
                    local tbl = s_to_t(command[3])
                    if type(tbl) == "table" then
                        for i,v in pairs(tbl) do
                            print(command[3].."."..i.." = "..tostring(v))
                        end
                    else
                        print(command[3].." = "..tostring(tbl))
                    end
         


        #4 Iryoku

        Iryoku

          Advanced Member

        • Windower Staff
        • 488 posts

          Posted 08 September 2015 - 10:19 PM

          First, like Arcon said, use loadstring to do this, don't try to parse it manually, you're going to get it wrong.

          ... 
          elseif command[1]:lower() == 'show' and (command[2]:lower() == 'variable' or command[2]:lower() == 'var') then
              local tbl = loadstring(command[3])()
              if type(tbl) == "table" then
                  for i,v in pairs(tbl) do
                      print(command[3].."."..i.." = "..tostring(v))
                  end
              else
                  print(command[3].." = "..tostring(tbl))
              end
          ...
          

           

          Second, is there a reason you don't want to use the eval addon for this? You can add a function to pretty print a table to your bootstrap and then just do //eval tprint(windower.ffxi.get_mob_by_target('t'))



          #5 sdahlka

          sdahlka

            Advanced Member

          • Members
          • PipPipPip
          • 324 posts

            Posted 09 September 2015 - 12:15 AM

            i ended up going with this

             

            elseif command[1]:lower() == 'show' and (command[2]:lower() == 'variable' or command[2]:lower() == 'var') then
                        local tbl = loadstring("return "..command[3])() or loadstring("return user_env."..command[3])()
                        if type(tbl) == "table" then
                            for i,v in pairs(tbl) do
                                print(command[3].."."..i.." = "..tostring(v))
                            end
                        else
                            print(command[3].." = "..tostring(tbl))
                        end
             

            and this is so i can see what the current variables and tables are in gearswap and also system variables are on the fly with out eval loaded






            1 user(s) are reading this topic

            0 members, 1 guests, 0 anonymous users