DX Unified Infrastructure Management

  • 1.  LUA -- parsing text into a table

    Posted Aug 19, 2012 03:57 PM

    Hello--

     

    I am trying to parse a list of profiles, from the net_connect probe, and place them into a table. I apparently don't understand the logic of my request, since the table value returns as nil, when I print it. My code is below. Any suggestions are appreciated.

     

    Kind regards.

     

    --Global declarations all_profile_names = {}  --Connect to probe. Edit this line with the address of your hub you wish to query addr = "/NIX/PrimaryHUB/hub1/controller"  --Command to request configuration of probe command = "probe_config_get"  --Build PDS answer file when probe_config_get asks for probe name args = pds.create() pds.putString(args, "name", "net_connect")  --Send request and store data in h_resp{} h_resp,rc = nimbus.request(addr, command, args) defaults = h_resp["/setup/default_host"] full_profiles = h_resp["/profiles"]  print("Test1") for k, v in pairs (h_resp) do    if substr(k, "/profiles/%a") then -- trying to pull just the names section       all_profile_names = string.sub("/profiles/%a", 10) --assign names to all_profile_names table    end end print("Test2") for k,v in pairs(all_profile_names) do    print (k.." : ", v)--this returns: end print(all_profile_names[1])


  • 2.  Re: LUA -- parsing text into a table
    Best Answer

    Posted Aug 20, 2012 09:06 AM

    A couple of issues:

     

    • The substr() function does not take a pattern as an argument. This would would though: substr(k, "/profiles/")
    • Using the substr() function was not specific enough in this case because it would match both the profile section and any subsections (like <services>) within each profile.
    • The string.sub() function needed a variable as an argument.
    • You need to use the table.insert() function (or t[#t+1]) to add a value to a table.

    Here is an update that should work better:

     

    --Global declarations
    all_profile_names = {}
    
    --Connect to probe. Edit this line with the address of your hub you wish to query
    addr = "/NIX/PrimaryHUB/hub1/controller"
    
    --Command to request configuration of probe
    command = "probe_config_get"
    
    --Build PDS answer file when probe_config_get asks for probe name
    args = pds.create()
    pds.putString(args, "name", "net_connect")
    
    --Send request and store data in h_resp{}
    h_resp,rc = nimbus.request(addr, command, args)
    defaults = h_resp["/setup/default_host"]
    full_profiles = h_resp["/profiles"]
    
    print("Test1")
    for k, v in pairs (h_resp) do
       if string.match(k, "^/profiles/[^/]+$") then -- trying to pull just the names section
          table.insert(all_profile_names, string.sub(k, 11)) --assign names to all_profile_names table
       end
    end
    print("Test2")
    for k,v in pairs(all_profile_names) do
       print (k.." : ", v)--this returns:
    end
    print(all_profile_names[1])


  • 3.  Re: LUA -- parsing text into a table

    Posted Aug 20, 2012 04:36 PM

    Brilliant. Thank you. I never would have figured that out. I had no idea those functions existed or how most of them work.

     

    It's going to take me some time to understand those character classes. I barely understand regex.

     

    Thanks again.



  • 4.  Re: LUA -- parsing text into a table

    Posted Aug 20, 2012 04:59 PM

    I find the PDF reference linked here to be very useful:

     

    http://lua-users.org/wiki/LuaShortReference

     

    It is very concise, so it is probably not a very good learning tool. But it is a very good reference, especially when you need to look up the functions that are available in standard Lua. I use it quite a bit for that and for checking I am using the right character classes in patterns. A more detailed reference is the official Lua language reference:

     

    http://www.lua.org/manual/5.1/

     

    If you want to take some time to learn more about Lua in general, these references are probably not the way to go. There are a few good books available, and I learned a lot from the Programming in Lua book. You can purchase the second edition, which covers Lua 5.1, and the first edition (for Lua 5.0) is available online and still very relevant:

     

    http://www.lua.org/pil/