title: Lua: How to Properly Construct Nested Functions
body: <p>I'm trying to create a function with local functions within it.  The main function would receive an output from an outside source, and the functions within it would be required to translate that input and return results for later use.  My problem is that the way I am currently attempting this, when I try to put in my first local function inside the main function, I continue to get <code>nil</code>.  Here is an example:</p>
  
  <pre><code>function stats(input)
      height, weight, name, age, gender, relate = string.match(input, "(%d*)ft,(%d*)lbs,(%w*),(%d*),(%u*),(%u)")
      if name then
          function nameInit(relate)
              relateTable = {["F"] = "Friend", ["R"] = "Relative"}
              for k,v in pairs (relateTable) do
                  if relate == k then
                      relship = v
                      return relship
                  end
              end
          end
      end
      person = name.." is "..age.." years old, weighs "..weight.." and blah blah blah....
      return person
  end
  print (stats("5.8ft, 160lbs, Mike Scott, 19, M, F"))
  </code></pre>
  
  <p>Obviously, this subject isn't practical but what I'm trying to do is along the same lines in terms of end response.  I'm currently getting <code>lua: filename: attempt to concatenate global 'relship' (a nil value)</code>?  I can get the response I want without the nested function.  But when I try to elaborate more on the response I would like to receive, and place that function inside the global function, I begin to get these response(s).  This seems to be my problem anytime I attempt to use functions within other functions.  I can make two separate global functions and print results from either one.  But the minute I try to use one within another, I screw myself up.  Anyone who can take some time to help a beginner better understand what he is doing wrong would be great!  Thanks all. </p>