2

i'd like to know if there is any possibility to read out dynamic variable names? Since the programm that passes the variables to my script calls them just "in1, in2, in3" etc. Hopefully there is any way to make a loop, because it is pretty annoying to handle every input separately...

Here is what i've tried so far, but it just gives me an error.

for i=1,19,2 do
myvar[i] = ["in"..i]
end 

I'm quite new to Lua, but i hope the solution is not that difficult :D

Edit: Oh I'll try to give you some more information. The "Main" Program is no not written in Lua and just set theese "in1 ... " variables. It is a kind of robotic programmic software and has a lot of funktions build in. Thats the whole thing so i can not simply use other variable names or an array. So it is not a function or anything else related to Lua... Here is a little Screenshot http://www.bilderload.com/daten/unbenanntFAQET.jpg At the moment the Lua script just passes the the first input.

3
  • It would help if you printed the exact error that you were getting instead of just saying "it just gives me an error". Commented Feb 28, 2012 at 15:46
  • Sorry here it is: "Line 2: unexpected symbol near '['" Commented Feb 28, 2012 at 16:24
  • Can't you have it export those variables into a table? That would simplify matters a great deal. Commented Feb 29, 2012 at 6:50

3 Answers 3

5

It depends on what you mean by "dynamic variable names."

The names of local variables do not exist. Local variables are any variable declared as a function parameter or with the local keyword. Local variables are compiled into offsets into the Lua stack, so their names don't exist. You can't index something by name to get them.

Global variables are members of the global table. Therefore, these ways to set a global variable are equivalent:

globalVar = 4
_G.globalVar = 4
_G["globalVar"] = 4

Since the programm that passes the variables to my script calls them just "in1, in2, in3" etc.

The program that passes variables to your script doesn't get to name them. A variable is just a placeholder for a value. It has no ownership of that value. When your function gets arguments, your function gets to name them.

You haven't said much about the structure of your program, so I can't really give good advice. But if you just want to take some number of values as parameters and access them as inputs, you can do that in two ways. You can take a table containing values as a parameter, or you can take a varargs:

function MyFunc1(theArgs)
  for i, arg in ipairs(theArgs) do
    --Do something with arg.
  end
end

function MyFunc2(...)
  for i, arg in ipairs({...}) do
    --Do something with arg.
  end
end

MyFunc1 {2, 44, 22} --Can be called with no () because it takes a single value as an expression. The table.
MyFunc2(2, 44, 22)
Sign up to request clarification or add additional context in comments.

7 Comments

Oh I'll try to give you some more information. The "Main" Program is no not written in Lua and just set theese "in1 ... " variables. It is a kind of robotic programmic software and has a lot of funktions build in. Thats the whole thing so i can not simply use other variable names or an array. So it is not a function or anything else related to Lua...
@user1238052: First, you need to put this into your question. Use the "edit" button. Second, how does it set those variables? Show us the code it's using to set them, and show us how your Lua function gets called.
That is the problem, there is no other Lua code! I've made a little screenshot to make the situation clear: link
@user1238052: I didn't say to post Lua code. I said to post code. Something must execute this Lua script. Post what does that and how it passes data.
This lua script is alway executed if one of the inputvalues change. I'm sorry but I can't provide you more information about this process since i don't have the source of the programm that passes the input.
|
1

Whoever wrote the code that spits out these "dynamic variables" didn't do a good job. Having them is a bad habit, and might result in data loss, cluttering of the global name space, ...

If you can change it, it'd be much better to just output a table containing the results.

That said, you're not to far off with your solution, but ["in"..i] is no valid Lua syntax. You're indexing into nothing. If those variables are globals, your code should read:

for i=1,19,2 do
    myvar[i] = _G["in"..i]
end

This reads the values contained by your variables out of the global table.

1 Comment

Hmm i tried it, but it seems that the variables are not global ones. I wrote this little piece of code on my own, but i haven't done anything with Lua up to now. I just need it to simplify some logic elemets.
1

Try this

myvar={ in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14, in15, in16, in17, in18, in19 }

if the variables are passed as global variables, or this

myvar = {...}

if the variables are passed as arguments to the script.

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.