3

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 nil. Here is an example:

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"))

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 lua: filename: attempt to concatenate global 'relship' (a nil value)? 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.

6
  • 3
    Where are you calling nameInit? You're only defining it here, but nothing is calling it. Commented May 14, 2014 at 21:32
  • 1
    Also, why not change stats(input) to stats(ht, wt, name, age, gender, relate) instead of using string.match to match them all up? Commented May 14, 2014 at 23:42
  • 2
    Going along with @ColonelThirtyTwo said you also aren't concatenating relship anywhere in that code snippet so it can't be generating that error. You need to provide us with a more accurate and complete code listing if you want to get help. (As a general tip you also want to use local variables, including functions, instead of using globals everywhere.) Commented May 15, 2014 at 1:04
  • @EtanReisner Sorry for not putting in the locals. In my code I do use it. When I typed my example code I completely neglected to do that. My apologies. But I do appreciate your help! Commented May 15, 2014 at 13:17
  • @Josh I don't know of another way to take an input, from an outside source, and match up the variables I want with the input received? Commented May 15, 2014 at 13:28

1 Answer 1

4

Based on your statement "the functions within it would be required to translate that input and return results for later use", I'm not sure that nested functions is what you want. You say that when you have two global functions your code works:

function func1(args)
    ...
end

function func2(args)
    ...
end

but when you nest (for example) func1 inside func2, it no longer works. Lua does allow you to define nested functions, but I can only think of two reasons to use them:

  1. to return a function that encapsulates a task, usually with some of the wrapper function's args and/or locals as upvalues.
  2. to encapsulate some logic in a function to be called from within the wrapper function, with no need for any other functions to call it.

For example of case 1:

function func2(a, b, c)
   function func1()
       do something with a, b, c eventhough they are not args of func1
       return result
   end
   return func1
end

someFunc = func2(1,2,3)
....
result = someFunc() -- calls func1 created inside func2, using 1,2,3 

For example of case 2:

function func2(a, b, c)
   function func1()
       do something with a, b, c eventhough they are not args of func1
       return result
   end
   result = func1()
   ...
end

func2(1,2,3)

You could also add a nested function to a table object (class) passed as argument, but I see this as a variation on case 1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.