title: Lua parsing a table path string to a table
body: <p>I need to parse a string that represents the path of a table, and turn it into the actual table to find a value nested inside a table structure:</p>
  
  <pre><code>local function strsplit(str, sep)
      sep = sep or "%s";
      local tbl = {};
      local i = 1;
      for str in string.gmatch(str, "([^"..sep.."]+)") do
          tbl[i] = str;
          i = i + 1;
      end
      return tbl;
  end
  
  local function parser(path, tbl)
      local value;
      local keys = strsplit(path, ".");
  
      for _, key in pairs(keys) do
          value = (value and value[key]) or tbl[key];
      end
      return value;
  end
  
  local data = {
      colors = {
          r = 1, g = 1, b = 1
      }
  }
  
  local red = parser("colors.r", data);
  print(red); -- prints 1 (correct!)
  </code></pre>
  
  <p>This code runs perfectly fine with the expected output. However, I would like to alter the "parser" function so that it also works with number index values and not just with strings as keys (the index part of the table, not just the hash table):</p>
  
  <pre><code>local data = {
      list = {
          50, 40, 30, 20, 10
      }
  }
  
  local value = parser("colors.list[2]", data);
  print(value); -- should print 40
  </code></pre>
  
  <p>When splitting the string representing the table path, one of the keys will become "list[2]" and this should be converted to a real table. Therefore, I will need some sort of regex matching solution that will be able to identify the square brackets and work it's magic; I just don't know where to get started with this.</p>